I'm trying to initialize an IntArray
in Kotlin like so:
intArrayOf(1..9)
But I get a TypeError
that Int
is required, but I'm providing an IntRange
. Is there a way to initialize the array with a range, or do I have to explicitly write out each value?
To initialize primitive arrays with a specific value, you can use the class constructor with lambda. For example, IntArray(5) creates an integer array of size 5 and initializes it with a value of 1 .
IntArray is a class in Kotlin representing the array of elements. Each instance of this class is represented as an integer array. To the constructor of this class you need to pass the number of elements you need in the array (size). By default, all the elements of the created array will be initialized to "0".
In Kotlin, arrays can be created using the function arrayOf() or using an Array constructor. Arrays are stored in a sequence as per the memory location is concerned. All the elements in an array can be accessed using their index. Arrays are mutable in nature.
In Kotlin There are Several Ways. Then simply initial value from users or from another collection or wherever you want. var arr = Array(size){0} // it will create an integer array var arr = Array<String>(size){"$it"} // this will create array with "0", "1", "2" and so on.
All the items stored in a Kotlin array are called elements that can be accessed with the help of their index numbers starting from 0. Using the arrayOf () function and array constructor. Initialize an array with values while declaring it.
An array stores multiple items together for easy sorting and searching through a collection of elements. All the items stored in a Kotlin array are called elements that can be accessed with the help of their index numbers starting from 0. Using the arrayOf () function and array constructor.
Kotlin has a built-in arrayOf method that converts the provided enumerated values into an array of the given type: 3. Primitive Arrays We can also use the arrayOf method with primitive values. However, Kotlin will autobox the primitive values to their corresponding object wrapper classes which will have detrimental performance implications.
To avoid this overhead Kotlin has wide support for primitive arrays. There are dedicated arrayOf methods for the following types: double, float, long, int, char, short, byte, boolean. We can easily initialize a primitive int array using its dedicated arrayOf method: 4. Late Initialization With Indices
Using built in functions, this is how you could get to an IntArray
from an IntRange
:
val array: IntArray = (1..9).toList().toIntArray()
This is a bit wasteful, because it first constructs a list where it puts all the elements, and then it constructs an array as well. To do this directly, you could use your own extension, something like...
fun IntRange.toIntArray(): IntArray {
if (last < first)
return IntArray(0)
val result = IntArray(last - first + 1)
var index = 0
for (element in this)
result[index++] = element
return result
}
Which would give you this syntax:
val array: IntArray = (1..9).toIntArray()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With