Kotlin Array

7. Kotlin Array 7.1 Kotlin Create Array 7.2 Create empty array 7.3 Create uninitialized array 7.4 Retrieving array value 7.5 Modify array value 7.6 Reversing array value 7.7 Get lastindex of an array 7.8 Array any function 7.9 Array count 7.10 Array append element

Kotlin Create Array

An array is a collection of key/value pairs. We can store more than one item in only one variable, i.e., Array. So, an array is used when there is a requirement to add more items in a single variable.

Kotlin provides two ways to create arrays - arrayOf() and array() constructor.

Example of Kotlin arrayOf()

fun main(args : Array<String>) {
    val x = arrayOf(22,12,43)
    for (a in x) {
        println(a)
    }
}
Output
22
12
43

Example of Kotlin array() constructor

fun main(args : Array<String>) {
    val x = Array<Int>(5){0}
    var y = Array(5, { i -> i * 3 })
    for(a in x) {
        println(a)
    }
    for(b in y) {
        println(b)
    }
}
Output
0
0
0
0
0
0
3
6
9
12

Kotlin provide methods to create arrays of primitive type, like byteArrayOf(), intArrayOf(), shortArrayOf(), longArrayOf(), charArrayOf, floatArrayOf(), doubleArrayOf().

Example

val a = intArrayOf(4,5,7)
val c = charArrayOf('x','y','z')
for(x in a) {
	println(x)
}
for(y in c) {
	println(y)
}
Output

4
5
7
x
y
z




Create empty array

In Kotlin, we can create an empty array with the help of emptyArray() function.

val empty = emptyArray()	

Create uninitialized array

In Kotlin, we can create an uninitialized array or declare it to nullable using arrayOfNulls() function. This can be initialized later.

fun main(args : Array<String>)  {   
	var x = arrayOfNulls <Int>(10)
    for(a in x) {
        println(a)
    }
}
Output
null
null
null
null
null
null
null
null
null
null

In the above example, we have created an array of 10 integers and all set to a null value.


Retrieving array value

Generally, in other programming languages, we have used the array index to retrieve the element. In addition to the square brackets, Kotlin also provides get() method to access array elements.

fun main(args : Array<String>)  {  
	val arr = arrayOf(22,12,43)
    println(arr[0])
    println(arr[2])
    println(arr.get(1))
}
Output
22
43
12




Modify array value

The set() method of Kotlin is used to modify the array value.


fun main(args : Array<String>)  {  
	val arr = arrayOf(22,12,43)
	arr.set(2,30)
}
Output
22
12
30

Reversing array value

Kotlin provides reverse() method to reverse an array elements.

fun main(args : Array<String>)  {  
	val arr = arrayOf(22,12,43)
	arr.reverse()
	for(ele in arr)
	{
		println(ele)
	}
}
Output
43
12
22

Get lastindex of an array

Kotlin provides lastIndex() method to get last element index of an array.

fun main(args : Array<String>)  {  
    val arr = arrayOf(22,12,43)
    println(arr.lastIndex)
}
Output
2

Array any function

It is used to check existence of any element in an array.

fun main(args : Array<String>)  { 
val arr = arrayOf(22,12,43)
if(arr.any())
 println(true)
else 
 println(false) 
}
Output
true

Array count

The array count() function returns the number of elements in an array.

fun main(args : Array<String>)  { 
	val arr = arrayOf(22,12,43)
    var x = arr.count()
    println("$x")
}
Output
2

Array append element

In Kotlin, the plus() or plusElement() method is used to append element at the end of an array.

fun main(args : Array<String>)  {  
    val arr = arrayOf(22,12,43)
    val x = arr.plus(50)
    for(e in x)
    {
        println(e)
    }
}
Output
22
12
43
50






Read more articles