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.

Creating Arrays in Kotlin

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.

Primitive type Arrays

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()

  • charArrayOf()

  • shortArrayOf()

  • longArrayOf()

Get and Set the Elements of an Array

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.

Example

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:

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 Array Length

Kotlin provides array property called size which returns the size i.e. length of the array.

Example

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.

Loop Through an Array

We can use for loop to loop through an array.

Example

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

Check if an Element Exists

We can use the in operator alongwith if...else to check if an element exists in an array.

Example

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

Distinct Values from Array

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.

Example

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

Dropping Elements from Array

We can use drop() or dropLast() member functions to drop elements from the beginning or from the last respectively.

Example

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

Checking an Empty Array

We can use isEmpty() member function to check if an array is empty or not. This function returns true if the array is empty.

Example

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

Kotlin Array Class

Kotlin's Array class defines the most fundamental data structure or collection, which works on contiguous index values.

The Kotlin's Array class is a generic collection that contains references to other objects. It's an ordered collection of items that can be accessed using their respective indices. In kotlin array variables can be created using the arrayOf(), arrayOfNulls(), or the emptyArray() standard library functions.

Constructor

Kotlin Array class has a single Constructor −

Sr.No. Constructor & Description
1 <init>

This Constructor creates a new array with the specified size, where each element is calculated by calling the specified init function.

Properties

Following are the Properties defined in the Kotlin Array class −

Sr.No. Properties & Description
1

size

It Returns the number of elements in the array.

2

indices

It returns the range of valid indices for the array.

3

lastIndex

It returns the last valid index for the array.

Functions

Following are the functions defined in the kotlin Array class −

Sr.No. Function & Description
1 get()

Returns the array element at the specified index.

2 set()

Sets the array element at the specified index to the specified value.

3 iterator()

Creates an iterator for iterating over the elements of the array.

4 all()

Returns true if all elements match the given predicate.

5 any()

Returns true if array has at least one element.

6 average()

Returns an average value of elements in the array.

7 binarySearch()

Searches the array or the range of the array for the provided element using the binary search algorithm.

8 component1()

Returns 1st element from the array.

9 component2()

Returns 2nd element from the array.

10 component3()

Returns 3rd element from the array.

11 component4()

Returns 4th element from the array.

12 component5()

Returns 5th element from the array.

13 contains()

Returns true if element is found in the array.

14 count()

Returns the number of elements in the array.

15 distinct()

Returns a list containing only distinct elements from the given array.

16 last()

Returns the last element.

17 distinctBy()

Returns a list of the distinct elements from the given array or collection.

18 drop()

Returns a list containing all elements except first n elements.

19 dropLast()

Returns a list containing all elements except last n elements.

20 dropLastWhile()

Returns a list containing all elements except last elements that satisfy the given predicate.

21 dropWhile()

Returns a list containing all elements except first elements that satisfy the given predicate.

22 elementAtOrElse()

Returns an element at that index If the given index is present in the list of collection.

23 elementAtOrNull()

Returns an element at the given index or null if the index is out of bounds of this array.

24 filter()

Returns a list containing only elements matching the given predicte.

25 filterNot()

Returns a list containing all elements not matching the given predicate.

26 filterNotNull()

Returns a list containing all elements that are not null.

27 find()

Returns the first element matching the given predicate, or null if no such element was found.

28 findLast()

Returns the last element matching the given predicate, or null if no such element was found.

29 first()

Returns the first element.

30 flatten()

Returns a single list of all elements from all array in the given array.

31 forEach()

Performs the given action on each elements.

32 getOrElse()

Returns an element at the given index or the result of calling the defaultValue.

33 getOrNull()

Returns an element at the given index or null if the index is out of bounds of this array.

34 groupBy()

Groups elements of original array by the key returned by the given keyslector function.

35 indexOf()

Returns first index of element, or -1 if the array does bot contain element.

36 indexOfFirst()

Returns index of the first element matching the given predicate, or -1 if the array does not contain such element.

37 indexOfLast()

Returns index of the last element matching the given predicate, or -1 if the array does not contain such element.

38 intersect()

Returns a set containing all elements that are contained by both this array and the specified collection.

39 isEmpty()

Returns ture if the array is empty.

40 isNotEmpty()

Returns true if the array is not empty.

41 isNullOrEmpty()

Returns true if this nullable array is either null or empty.

42 joinTo()

Appends the string from all the elements separated using separator.

43 joinToString()

Creates a string from all the elements separated using separator.

44 lastIndexOf()

Returns last index of element, or -1 if the array does not contain element.

45 lastOrNull()

Returns the last element, or null if the array is empty.

46 map()

Returns a list containing the results of applying the given transform function to each element in the original array.

47 maxOf()

Returns the largest value among all values produced by selector function.

48 minOf()

Returns the smallest value among all values produced by selector function.

49 none()

Returns true if the array has no elements.

50 partition()

Splits the original array into pair of lists.

51 random()

Returns a random element from this array.

52 randomOrNull()

Returns a random element from this array, or null if this array is empty.

53 reduce()

Helps to convert elements of an array into a single result.

54 reduceRight()

Accumulates value starting with the last element and applying operation from right to left to each element and current accumulator value.

55 reverse()

Reverses elements in the array in-place.

56 reversed()

Returns a list with elements in reversed order.

57 runningReduce()

This function is used to perform a successive accumulation operation on an array.

58 shuffle()

Randomly shuffles elements in this array in-place.

59 single()

Returns the single element, or throws an exception if the array is empty or has more than one element.

60 slice()

Returns a list containing elements at indices in the specified indices range.

61 sliceArray()

Returns an array containing elements of this array at specified indices.

62 sortBy()

Sorts elements in the array in-place according to natural sort order of the value returned by specified selector function.

63 sortByDescending()

Sorts elements in the array in-place descending according to natural sort order of the value returned by specified selector function.

64 sortDescending()

Sorts elements in the array in-place descending according to natural sort order.

65 sorted()

Returns a list of all elements sorted according to their natural sort order.

66 sortedArray()

Returns an array with all elements of this array sorted according to their natural sort order.

67 sortedArrayDescending()

Returns an array with all elements of this array sorted descending according to their natural sort order.

68 sortedBy()

Returns a list of all elements sorted according to natural sort order of the value returned by specified selector function.

69 sortedByDescending()

Returns a list of all elements sorted descending according to natural sort order of the value returned by specified selector function.

70 sortedDescending()

Sorts the array's element or collection in descending order according to the natural order.

71 sortedWith()

Sorts the array's element or collection in to the sorted order according to the specified comparator or condition.

72 sortWith()

Sorts the array's element or collection in a given range according to the order specified by the given comparator or condition.

73 subtract()

Removes all elements from the first array that match elements in the second array.

74 sum()

Returns the sum of all elements of the array and collection.

75 sumOf()

Displays the total of all array and collection items using a selector function applied to each element of the collection.

76 take()

Returns a list containing first n elements.

77 takeLast()

Returns a list containing last n elements.

78 takeLastWhile()

Returns a list containing last elements satisfying the given predicate.

79 takeWhile()

Returns a list containing first elements satisfying the given predicate.

80 toList()

Returns a List containing all elements.

81 toSet()

Returns a Set of all elements.

82 union()

Returns a set containing all distinct elements from both collections.

Quiz Time (Interview & Exams Preparation)

Answer : D

Explanation

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

Answer : C

Explanation

Kotlin index starts from 0, so at 2 index we will find 3rd element which is Banana.

Answer : D

Explanation

Option D is correct because we can get the size of an array using either size property or count() function.

Advertisements