Arrays in Kotlin
In this article, we are going to discuss the arrays in Kotlin programming language.
In Kotlin arrays are just classes. Unlike Java, Kotlin doesn’t have a special syntax for declaring array types.
No special syntax but still there are various ways to declare the arrays in Kotlin.
First, let us get to know about the Array class in Kotlin and that has set and get functions and size property along with other useful functions like:
Now, to create an array we are going to use various library functions or ways here:
- arrayOf():
Output:
[1, 2, 3, 4, 5]
[1, 2.0, 3, Kotlin, 5]
As we can see above, arrayOf() can hold different values of different types.
If we want to create an array of a given size filled with null elements then we can use arrayOfNulls().
2. Array(size: Int, init: (Int) -> T): This is the inline constructor of Array class in Kotlin which accepts the size of an array and the function that can return the initial value of each array element given its index like:
Output:
0
1
4
9
16
3. xxxArrayOf(): With this, we can easily define the primitive arrays in Kotlin like:
Output:
1 2 3
Above implementation returns IntArray and similarly, we can define other primitive types arrays as well like:
/**
* Returns an array containing the specified [Double] numbers.
*/
public fun doubleArrayOf(vararg elements: Double): DoubleArray
/**
* Returns an array containing the specified [Float] numbers.
*/
public fun floatArrayOf(vararg elements: Float): FloatArray
/**
* Returns an array containing the specified [Long] numbers.
*/
public fun longArrayOf(vararg elements: Long): LongArray
/**
* Returns an array containing the specified characters.
*/
public fun charArrayOf(vararg elements: Char): CharArray
/**
* Returns an array containing the specified [Short] numbers.
*/
public fun shortArrayOf(vararg elements: Short): ShortArray
/**
* Returns an array containing the specified [Byte] numbers.
*/
public fun byteArrayOf(vararg elements: Byte): ByteArray
/**
* Returns an array containing the specified boolean values.
*/
public fun booleanArrayOf(vararg elements: Boolean): BooleanArray
Note: Kotlin also has the specialized class to represent arrays of primitive types without boxing overhead:
xxxArray(): This accepts the size and/or function that can return the initial value of each array element given its index like:
With size only:
Output:
0 0 0
With size and function to initialize with the custom values:
Output:
11 11 11
Similarly, for other primitive types, we have:
//For Byte.
public class ByteArray(size: Int) {
public inline constructor(size: Int, init: (Int) -> Byte)
}//For Char.
public class CharArray(size: Int) {
public inline constructor(size: Int, init: (Int) -> Char)
}//For Short.
public class ShortArray(size: Int) {
public inline constructor(size: Int, init: (Int) -> Short)
}//For Long.
public class LongArray(size: Int) {
public inline constructor(size: Int, init: (Int) -> Long)
}//For Float.
public class FloatArray(size: Int) {
public inline constructor(size: Int, init: (Int) -> Float)
}//For Double.
public class DoubleArray(size: Int) {
public inline constructor(size: Int, init: (Int) -> Double)
}//For Boolean.
public class BooleanArray(size: Int) {
public inline constructor(size: Int, init: (Int) -> Boolean)
}
That’s it in this article for arrays in Kotlin. If you’ve liked this article, please don’t forget to hit the clap button. Thanks in advance :)