To hold multiple values in kotlin we use a data type Array.
When you want to store multiple values like 1,2,3 in kotlin Arrays are used.
How to create an array in Kotlin?
arrayOf() can be used to create a array object in kotlin
Example :
var intArray = arrayOf(1,2,3)
Explanation :
var intArray – name given to access the array object
arrayOf(1,2,3) – initialising with values 1,2,3
Are arrays in kotlin mutable ?
Arrays in kotlin are mutable, meaning you can edit the items in an array by accessing them.
How to access items in an array ?
Using Indexes – Index is nothing but position of an item in an array
Example :
fun main (args:Array<String>)
{
var intArray = arrayOf(6,7,8)
println("First item in the array is "+intArray[0])
}
Output :
First item in the array is 6
Explanation :
For each item in an array an object is created and reference is given to a index.
Indexes start from 0,
First item with value 6, an object of type Int is created and the reference is given to index 0 .
second item with value 7, an object of type Int is created and the reference is given to index 1 .
third item with value 8, an object of type Int is created and the reference is given to index 2 .
intArray[0] – points to first item in an array
Array objects are declare as var or val ?
var – An array declared using var can refer to a different array objects of similar type
val – An array declared using val holds reference to a same object forever
Example:
fun main (args:Array<String>)
{
var firstArray = arrayOf(1,2,3)
firstArray = arrayOf(4,5)
println("First item in the array is "+firstArray[0])
}
Output :
First item in the array is 4
Explanation :
var firstArray = arrayOf(1,2,3) -> Array of type Int is created and reference is given to ‘firstArray’
firstArray = arrayOf(4,5) -> A new array object is created and ‘firstArray’ will now pointing to this new object
Using var with array’s let’s you point to different array object’s of similar types.
Example:
fun main (args:Array<String>)
{
val firstArray = arrayOf(1,2,3)
firstArray = arrayOf(4,5)
println("First item in the array is "+firstArray[0])
}
Output :
Val cannot be reassigned
Explanation :
val firstArray = arrayOf(1,2,3) -> Array of type Int is created and reference is given to ‘firstArray’
firstArray = arrayOf(4,5) -> trying to point ‘firstArray’ to new array object
As val is used , firstArray holds reference to the same array forever and does not let you point to any other array’s
When array is declared using val, can the items be updated ?
val when used with variable you wont be able to update the variable
val when used with array cannot refer to a new array object however items in an array can still be updated.
Example :
fun main (args:Array<String>)
{
val firstArray = arrayOf(1,2,3)
firstArray[0] = 6
println("First item in the array is "+firstArray[0])
}
Output :
First item in the array is 6
Explanation :
val firstArray = arrayOf(1,2,3) -> declare using val, holds reference to the same array forever
firstArray = arrayOf(4,5) -> this cannot be done
firstArray[0] = 6 -> As the same object can still be updated, updates the item at index 0 in the array successfully