Mutable Array

Array is one of the most fundamental data structure in practically all programming languages. Array is a collection of similar data either of types Int, String etc. Array in Kotlin has mutable in nature with fixed size. Which means we can perform both read and writes operations on elements of array.

Arrays are used to organize data in programming so that a related set of values can be easily sorted or searched. They are stored in contiguous memory locations and can be accessed programmatically through their indexes (array[1], array[0], etc.)

There are two ways to define an array in Kotlin.

  • With arrayOf() function
  • With Array constructor

With arrayOf() function

In Kotlin we can create Array object using arrayOf() function.

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	// implicity type declaration
	var data = arrayOf("John", "Hary", 34, 34f)
	
	//explicit type declaration
	var names = arrayOf<String>("John1", "Hary1")
	
	for(item in data) {
		when(item) {
			is String -> println("Name: ${item}")
			is Int -> println("Age: ${item}")
			is Float -> println("Salary: ${item}")
		}
	}
	
	println()
	for(item in names) {
		println("Name: ${item}")
	}
}

Output:
Name: John
Name: Hary
Age: 34
Salary: 34.0

Name: John1
Name: Hary1


Other factory methods available for creating arrays
  • byteArrayOf()
  • charArrayOf()
  • shortArrayOf()
  • longArrayOf()

With Array Construction

Since Array is a class in Kotlin, we can also use the Array constructor to create an array. The constructor takes two parameters Ist is array size and IInd accepts the index of a given element and returns the initial value of that element.

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var data = Array<String>(3) { i -> "Index: $i" }
	for (item in data) {
		println("${ item }")
	}

	println()
	
	var data1 = Array<String>(3) { i -> "" }
	data1[0] = "John Carlo"
	data1[1] = "Devid Son"
	data1[2] = "Harry Potter"

	for (item in data1) {
		println("${ item }")
	}
}

Output:
Index: 0
Index: 1
Index: 2

John Carlo
Devid Son
Harry Potter


Accessing and Modifying Arrays

In Kotlin we can access any array element using get() method and modify using set() method. and can be directly access or modify using [ index ] with Array.

Example:
package com.theitbulls.kotlinex

fun main(args: Array<String>) {
	var data = Array<String>(3) { i -> "Value at($i): ${i}}" }
	for (index in 0..data.size -1 ) {
		println("${ data.get(index )}")
	}
	
	// modifying data array
	for(index in 0..data.size - 1) {
		data.set(index, "value at (${index}): ${data.size - (index + 1)}")
	}

	println("After modified\n")
	for (index in 0..data.size -1 ) {
		println("${ data.get(index )}")
	}
	println()
	
	var data1 = Array<String>(3) { i -> "" }
	// modify data1 values using [index] 
	data1[0] = "John Carlo"
	data1[1] = "Devid Son"
	data1[2] = "Harry Potter"

	for (item in data1) {
		println("${ item }")
	}
}

Output:
Value at(0): 0}
Value at(1): 1}
Value at(2): 2}

After modified
value at (0): 2
value at (1): 1
value at (2): 0

John Carlo
Devid Son
Harry Potter