Introduction
Starting with the definition of Array, It is the collection or accumulation of similar kinds of data types. This means that collection of int type data is an example of integer type array, collection of char type data is character type array, collection of string type data is string type array, respectively.
Arrays in Kotlin are mutable in nature(unlike Strings), and that means we can perform basic operations(read and write)on the elements of an array. The concept of an array is not new in Kotlin because it is a fundamental data structure that comes almost in all programming languages.
The idea behind using arrays was to implement something that stores a collection of similar data types so that eventually, data elements can be sorted and searched quickly. So, arrays basically help us to store multiple values in a single variable, rather than to create separate variables for every single value. In this blog, we will be covering everything about the declaration, initialization, implementation, Examples, and operations of Arrays in Kotlin.
Defining an Array in Kotlin
Before looking at the Syntax to define an Array in Kotlin, first, we must know that there are two possible ways to define an Array in Kotlin:
- Using arrayOf() function
- Using array constructor
Using arrayOf() function
The first way is by using the inbuilt library function arrayOf() for the creation of an array by simply passing the values of the elements/content to the function.
Syntax:
// Use of arrayOf() implicitly
val num = arrayOf(1, 2, 3, 4) // implicit type is when compiler decides datatype
// Use of arrayOf() explicitly
val num = arrayOf<Int>(1, 2, 3) //explicit type is when user defines datatype
Example for Implicit type is given below.
Code:
fun main(){
val arrayName = arrayOf(10, 20, 30, 40, 50)
for (i in 0..arrayName.size-1)
{
print(" "+arrayName[i])
}
println()
}
Output:
10 20 30 40 50
Example for Explicit type is given below.
Code:
fun main(){
val arrayName = arrayOf<Int>(100, 200, 300, 400, 500)
for (i in 0..arrayName.size-1)
{
print(" "+arrayName[i])
}
}
Output:
100 200 300 400 500
Using array constructor
As we all know that Array is a class in Kotlin, So we can utilize the Array constructor to create an array.
Wonders how?
Well, it's simple; keep in mind that the constructor takes two parameters-
a. Size/maximum capacity of the Array
b. A function that takes the index of a specified element and returns the element's initial value.
Syntax:
val ans= Arr(10, {i-> i*1})
In this example of Syntax, we are passing the size of our Array (Arr in this case) as ten and a lambda expression which initializes the element values from 0 to 9.
For better understanding, See this example given below.
Code:
fun main() {
val arrName = Array(10, { i -> i * 1 })
for (i in 0..arrName.size-1)
{
println(arrName[i])
}
}
Output:
0
1
2
3
4
5
6
7
8
9
Also, note down the point that, Kotlin also has some built-in factory methods for the creation of arrays of primitive data types, such as byteArray, intArray, etc. Although these classes do not extend the Array class, they have the same methods and features.
Aside from these, Kotlin includes factory methods for creating arrays of primitive data types like byteArray, intArray, short Array, and so on. Although these classes do not extend the Array class, they have the same methods and features.
The factory method for creating an integer array, for example, is:
val ans = intArrayOf(1, 2, 3, 4, 5, 6, 7,8)
Similarly, Other factory methods for creating arrays in Kotlin are-
charArrayOf() // for storing array of characters(like a,b,c,d,...)
byteArrayOf() // for storing byte data
longArrayOf() // for storing large long type values (32-bit number)




