Array

Array

Array is one of the most fundamental data structure in practically all programming languages.
The idea behind an array is to store multiple items of the same data-type,such as an integer or string under a single variable name.
Arrays are used to organize data in programming so that a related set of values can be easily sorted or searched.
Here are some basic properties of arrays –
  • They are stored in contiguous memory locations.
  • They can be accessed programmatically through their indexes (array[1], array[0], etc.)
  • They are mutable.
  • Their size is fixed.
 

Creating an array –

In Kotlin, arrays are not a native data type, but a mutable collection of similar items which are represented by the Array class.
There are two ways to define an array in Kotlin.
 
Using the arrayOf() function –
We can use the library function arrayOf() to create an array by passing the values of the elements to the function.
Syntax:
val num = arrayOf(1, 2, 3, 4) //implicit type declaration val num = arrayOf<Int>(1, 2, 3) //explicit type declaration
 
fun main() { // declaring an array using arrayOf() val arrayname = arrayOf(1, 2, 3, 4, 5) for (i in 0..arrayname.size-1) { print(" "+arrayname[i]) } println() // declaring an array using arrayOf<Int> val arrayname2 = arrayOf<Int>(10, 20, 30, 40, 50) for (i in 0..arrayname2.size-1) { print(" "+arrayname2[i]) } } /* Output: 1 2 3 4 5 10 20 30 40 50 */
 
Using the Array constructor –
Since Array is a class in Kotlin, we can also use the Array constructor to create an array.
The constructor takes two parameters:
  1. The size of the array, and
  1. A function which accepts the index of a given element and returns the initial value of that element.
Syntax:
// lambda Expression val num = Array(3, {i-> i*1})
 
  • Apart from these, Kotlin also has some built-in factory methods to create arrays of primitive data types, such as byteArray, intArray, shortArray, etc. These classes do not extend the Array class; however, they implement the same methods and properties.
    • Other factory methods available for creating arrays:
    • byteArrayOf()
    • charArrayOf()
    • shortArrayOf()
    • longArrayOf()
    •  

Accessing and modifying arrays –

So far, we have seen how to create and initialize an array in Kotlin. Now, let’s see how to access and modify them.
Again, there are two ways of doing this:
Using get() and set() methods –
As you know, an array in Kotlin is basically a class. Therefore, we can access the data of a class object via its member functions. The get() and set() functions are said to be member functions.
 
  • The get() method takes a single parameter—the index of the element and returns the value of the item at that index.
Syntax:
val x = num.get(0)
  • The set() method takes 2 parameters: the index of the element and the value to be inserted.
Syntax:
num.set(1, 3)
 
  • The [ ] operator can be used to access and modify arrays.
To access an array element, the syntax would be:
val x = num[1]
This will assign the value of the second element in num to x.
To modify an array element, we should do:
num[2] = 5
This will change the value of the third element in the num array to 5
 
Another arguably less tedious Of Traversing array, way to do the above is using the foreach loop.
Syntax:
arrayname.forEach({index->println(index)})
Kotlin program of array traversal using foreach loop:
// Traversing an array fun main() { val arrayname = arrayOf<Int>(1, 2, 3, 4, 5) arrayname.forEach({ index -> println(index) }) }