Kotlin - Arrays

Arrays are used to store multiple items of the same data-type in a single variable, such as an integer or string under a single variable name.
For example, if we need to store names of 1000 employees, then instead of creating 1000 different string variables, we can simply define an array of string whose capacity will be 1000.
Like any other modern programming languages, Kotlin also supports arrays and provide a wide range of array properties and support functions to manipulate arrays.
To create an array in Kotlin, we use the arrayOf() function, and place the values in a comma-separated list inside it:
val fruits = arrayOf("Apple", "Mango", "Banana", "Orange")

Optionally we can provide a data type as follows:
val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange")

Alternatively, the arrayOfNulls() function can be used to create an array of a given size filled with null elements.
Kotlin also has some built-in factory methods to create arrays of primitive data types. For example, the factory method to create an integer array is:
val num = intArrayOf(1, 2, 3, 4)

Other factory methods available for creating arrays:
byteArrayOf()
byteArrayOf()
charArrayOf()
charArrayOf()
shortArrayOf()
shortArrayOf()
longArrayOf()
longArrayOf()
We can access an array element by using the index number inside square brackets. Kotlin array index starts with zero (0). So if you want to access 4th element of the array then you will need to give 3 as the index.
fun main(args: Array<String>) {
   val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange")
  
   println( fruits [0])
   println( fruits [3])
   
}

When you run the above Kotlin program, it will generate the following output:
Apple
Orange

Kotlin also provides get() and set() member functions to get and set the value at a particular index. Check the following example:
fun main(args: Array<String>) {
   val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange")
  
   println( fruits.get(0))
   println( fruits.get(3))
   
   // Set the value at 3rd index
   fruits.set(3, "Guava")
   println( fruits.get(3)) 
}

When you run the above Kotlin program, it will generate the following output:
Apple
Orange
Guava

Kotlin provides array property called size which returns the size i.e. length of the array. 
fun main(args: Array<String>) {
   val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange")
  
   println( "Size of fruits array " + fruits.size )

}

When you run the above Kotlin program, it will generate the following output:
Size of fruits array 4

We can also use count() member function to get the size of the array.
We can use for loop to loop through an array.
fun main(args: Array<String>) {
   val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange")
  
   for( item in fruits ){
      println( item )
   }
   
}

When you run the above Kotlin program, it will generate the following output:
Apple
Mango
Banana
Orange

We can use the in operator alongwith if...else to check if an element exists in an array.
fun main(args: Array<String>) {
   val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange")
  
   if ("Mango" in fruits){
      println( "Mango exists in fruits" )
   }else{
      println( "Mango does not exist in fruits" )
   }
   
}

When you run the above Kotlin program, it will generate the following output:
Mango exists in fruits

Kotlin allows to store duplicate values in an array, but same time you can get a set of distinct values stored in the array using distinct() member function.
fun main(args: Array<String>) {
   val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange", "Apple")
   
   val distinct = fruits.distinct()
   for( item in distinct ){
      println( item )
   }
}

When you run the above Kotlin program, it will generate the following output:
Apple
Mango
Banana
Orange

We can use drop() or dropLast() member functions to drop elements from the beginning or from the last respectively.
fun main(args: Array<String>) {
   val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange", "Apple")
   
   val result = fruits.drop(2) // drops first two elements.
   for( item in result ){
      println( item )
   }
}

When you run the above Kotlin program, it will generate the following output:
Banana
Orange
Apple

We can use isEmpty() member function to check if an array is empty or not. This function returns true if the array is empty.
fun main(args: Array<String>) {
   val fruits = arrayOf<String>()
   println( "Array is empty : " + fruits.isEmpty())
   
}

When you run the above Kotlin program, it will generate the following output:
"Array is empty : true

Q 1 - Which of the following is true about Kotlin Arrays?
A - Kotlin arrays are capable to store all data types.
B - Kotlin arrays can be created using either arrayOf() or arrayOfNulls() functions.
C - Kotlin provides a rich set of properties and functions to manipulate arrays.
D - All of the above
All the mentioned statements are correct about Kotlin Arrays.
Q 2 - What will be the output of the following code segment?
fun main(args: Array<String>) {
   val fruits = arrayOf<String>("Apple", "Mango", "Banana", "Orange")

   println( fruits [2])

}

A - Apple
B - Mango
C - Banana
D - None of the above
Kotlin index starts from 0, so at 2 index we will find 3rd element which is Banana.
Q 3 - How to get the size of a Kotlin array?
A - Using length() function
B - Using size property
C - Using count() function
D - B and C both
Option D is correct because we can get the size of an array using either size property or count() function.

    68 Lectures 
     4.5 hours 

 Arnab Chakraborty

    71 Lectures 
     5.5 hours 

 Frahaan Hussain

    18 Lectures 
     1.5 hours 

 Mahmoud Ramadan

    49 Lectures 
     6 hours 

 Catalin Stefan

    49 Lectures 
     2.5 hours 

 Skillbakerystudios

    22 Lectures 
     1 hours 

 CLEMENT OCHIENG
 Print
 Add Notes
 Bookmark this page