Summarize of Kotlin Arrays.
4. Arrays
1. Generic Arrays
Generic arrays in Kotlin are represented by Array<T>
To create an empty array, use emptyArray<T>()
factory function.
1
| val empty = emptyArray<String>()
|
Can use like below situation.
1
| return someInputArray?.filterNotNull() ?: emptyArray()
|
To create an array with given size and initial values, use the constructor.
1 2 3
| var arr = Array<String>(size = 5, init = {index -> "Item #$index"}) println(Arrays.toString(arr)) println(arr.size)
|
1 2 3 4 5
| emptyArr.set(1, "A") emptyArr.set(2, "B")
emptyArr[1] = "A" emptyArr[2] = "B"
|
2. Arrays of Primitives
Those types do not inherit from Array to avoid boxing, however, they have same attributes and method.
Kotlin type |
Factory function |
JVM type |
BooleanArray |
booleanArrayOf(true, false) |
boolean[ ] |
ByteArray |
byteArrayOf(1, 2, 3) |
byte[ ] |
CharArray |
charArrayOf(‘a’, ‘b’, ‘c’) |
char[ ] |
DoubleArray |
doubleArrayOf(1.2, 5.0) |
double[ ] |
FloatArray |
floatArrayOf(1.2, 5.0) |
float[ ] |
IntArray |
intArrayOf(1, 2, 3) |
int[ ] |
LongArray |
longArrayOf(1, 2, 3) |
long[ ] |
ShortArray |
shortArrayOf(1, 2, 3) |
short[ ] |
3. Create an array
Can create array as factory method.
4. Create an array using a closure
1
| val a = Array(3){ i -> i * 2 }
|
5. Create an uninitialized array
1
| val a = arrayOfNulls<Int>(3)
|