An array is used to store similar data type values in a single variable. For example, you may create fifty integer or string type variables and store a value in each.
Alternatively, you may create a single array of int or string types and store fifty values in it.
A few main points about Kotlin arrays:
- The array class represents an array in Kotlin.
- The array class has get and set functions.
- The array class also has the size property.
- Just like in Java, the items in the Kotlin array are called elements.
- Elements in the array can be accessed by index number.
- The index starts at zero.
- Kotlin arrays also have a few other useful member functions.
How to create an array in Kotlin?
You may create arrays in different ways:
By using the library function
You may use arrayOf() library function for creating an array. For example:
Similarly, you may use a specific type of library function for creating an array. These include:
- charArrayOf()
- booleanArrayOf()
- byteArrayOf()
- intArrayOf()
- longArrayOf()
- shortArrayOf()
You may also create an array as follows:
Or
An int array can also be created like this:
The following section gives more detail about how to create, initialize, and access array elements in Kotlin with code.
The examples also include using a few useful array functions.
An example of using arrayOf() function
In the first example of Kotlin arrays, I am using arrayOf() library function to create an array that contains five integer type elements.
This is followed by using a for loop for accessing those array elements:
fun main(args: Array<String>) { //Array Demo var NumArr = arrayOf(5,10,15,20,25) for(item in NumArr){ println(item) } }
The output:
5
10
15
20
25
The example of a string array by arrayOf function
The example below initializes an array of strings by using the arraOf() function. Notice, how the string data type is specified in the code below.
Again, I iterated through that array by using a for loop for displaying its items:
fun main(args: Array<String>) { //Array Demo var ArrCities = arrayOf<String>("New York","London","Dubai") for(item in ArrCities){ println(item) } }
The result:
New York
London
Dubai
Accessing array elements by index example
Just like in Java arrays, the index of array elements in Kotlin starts at zero. The example below shows accessing the array elements by index number:
fun main(args: Array<String>) { //Array Demo var ArrCountriies = arrayOf<String>("USA","UK","UAE") println("The country name at 0 index: " +ArrCountriies[0]) }
The output:
The example of array get() function
Now let us look at a few useful array functions and properties in Kotlin.
The first is the get() function that is used to return the array element at the specified index.
It takes an argument which is the index number (int type).
The example below shows using the get() function for accessing an element:
fun main(args: Array<String>) { //Array get() Demo var ArrNames = arrayOf<String>("Mike","Tina","Tipu", "Naman") println("0 index: " +ArrNames.get(0)) println("1 index: " +ArrNames.get(1)) println("2 index: " +ArrNames.get(2)) println("3 index: " +ArrNames.get(3)) }
The output:
The example of set() function
The set() function is used to set the array element value at the specified index position. It takes two arguments; first is the index and the other is the value to be set.
The example below shows how to use this function.
I have created an array of four elements of string type. By using the set() function, I will modify the third element value:
fun main(args: Array<String>) { //Array set() Demo var ArrLangs = arrayOf<String>("Kotlin","Java","Python", "PHP") //before set funciton for(item in ArrLangs){ println(item) } ArrLangs.set(2,"JavaScript") //after set function println("#######################") println("After using set function:") println("#######################") for(item in ArrLangs){ println(item) } }
The result:
The count function example
The count function returns the number of elements in this array. See the example below for its usage:
fun main(args: Array<String>) { //Array count() Demo var Arrlongs = arrayOf<Long>(1,2,3,5,66656,54334, 344545,6555543,4545454, 232,3223,23443,23232,34343,434334,3434,343,43,44) println("Total elements in the array: " +Arrlongs.count()) }
The output:
A distinct function example
If you require to get the list of distinct elements in an array then use the distinct() function.
An array is specified and distinct() returns the list of unique items.
The example below contains duplicate elements. I used distinct function and see how it returns the list of unique items:
fun main(args: Array<String>) { //Array count() Demo var ArrNums = arrayOf<Int>(5,4,3,2,3,5,1) var ArrChar = arrayOf<Char>('z', 'd', 'y', 'z', 't', 'd') println("Unique elements in ArrNum: " +ArrNums.distinct()) println("Unique elements in ArrChar: " +ArrChar.distinct()) }
The output of the above code:
Unique elements in ArrNum: [5, 4, 3, 2, 1]
Unique elements in ArrChar: [z, d, y, t]
Using drop and dropLast functions
Similarly, the dropLast returns a list of array elements except for the n from the last.
See the example of both functions below:
fun main(args: Array<String>) { //Array drop() and dropLast Demo var ArrNums = arrayOf<Int>(5,10,15,20,25,30,35, 40, 45, 50) println("After drop with 3 value: " +ArrNums.drop(3)) println("After dropLast with 3 value: " +ArrNums.dropLast(3)) }
The output of the above code:
After drop with 3 value: [20, 25, 30, 35, 40, 45, 50]
After dropLast with 3 value: [5, 10, 15, 20, 25, 30, 35]
first and last functions example
The first() function of the array returns the first element while the last() returns the last element of the array. Have a look at the example of both:
fun main(args: Array<String>) { //Array drop() and dropLast Demo var ArrNums = arrayOf<Int>(10,20, 30, 40, 50) println("First element in array: " +ArrNums.first()) println("Last element of array: " +ArrNums.last()) }
Output:
First element in array: 10
Last element of array: 50
An example of the size property
The size is a property of an array.
It returns the total number of elements in the array.
See the example code for its usage:
fun main(args: Array<String>) { //Array drop() and dropLast Demo var ArrChar = arrayOf<Char>('a', 'e', 'i', 'o', 'u') println("The size of array: " +ArrChar.size) }
Result:
Sorting the array by sort() function
The sort() function is used to sort a range in the array in place. For example:
fun main(args: Array<String>) { //Array drop() and dropLast Demo var ArrNum = arrayOf<Int>(50, 5, 25, 16, 21, 1, 100) println("###########Before Sorting Array#########") for(item in ArrNum){ println(item) } println("###########After Sorting Array#########") ArrNum.sort(0,6) for(item in ArrNum){ println(item) } }
The output:
You may learn more about the array functions on the Kotlin official website here.