How to Add New Element in Array In Kotlin ?

In this post, we will see how to add new element in array in Kotlin.

Suppose you have an array

 val arr = intArrayOf(1,2,3,4,5)
 

What if you want to add an item 10 in this array ?

Have you ever tried adding item in array like below –

 arr[5] = 10
 

or,

 arr.set(5, 10)
 

What did you get?
Did your program run ?

fun main() {

	val arr = intArrayOf(1,2,3,4,5)

	arr[5] = 10     // Trying to add an item at index = 5, Exception Occurred

	println("Elements in Array: ")

        // Traverse using for loop
	for(element in arr) {
		print(" $element")
	}
	println("")
}

When you run above program, output will be –

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at examples.array._4_add_item_arrayKt.main(4_add_item_array.kt:13)
at examples.array._4_add_item_arrayKt.main(4_add_item_array.kt)

This is because we are trying to access elements at 6th position but there are only 5 elements in array.
We can only set value by using index or .set method. We can n’t add a new element in existing array in kotlin.

Array in Kotlin are fixed size (Immutable). It means, once created it can not be resized.

If you want to resize array in kotlin dynamically, you need to use MutableList. Then you can add new element in array.

You can add new element in array in Kotlin in several ways –

1. Using MutableList

Pseudo algorithm –

  • Convert array into MutableList
  • Add specified element at end of the list
  • return new array that contains all the elements in the list

We can do above as below –

fun main() {

	var arr = intArrayOf(1,2,3,4,5)

	arr = addElement(arr, 10)

	println("Elements in Array: ")
	for(element in arr) {
		print(" $element")
	}
	println("")
}

fun addElement(arr: IntArray, element: Int): IntArray {
	val mutableArray = arr.toMutableList()
	mutableArray.add(element)
	return mutableArray.toIntArray()
}

When you run above program, output will be –

Elements in Array:
1 2 3 4 5 10

Note that 10 is new item in array arr.

Similarly, if you have defined array using arrayOf() function, you can do it as below –

fun main() {

	var arr = arrayOf(1,2,3,4,5)

	arr = addElement(arr, 10)

	println("Elements in Array: ")
	for(element in arr) {
		print(" $element")
	}
	println("")
}

fun addElement(arr: Array<Int>, element: Int): Array<Int> {
	val mutableArray = arr.toMutableList()
	mutableArray.add(element)
	return mutableArray.toTypedArray()
}

When you run above program, output will be –

Elements in Array
1 2 3 4 5 10

2. Using System.arraycopy

You can use System.arraycopy to add new elements in array. We can do it as below –

  • Create an array of size bigger than existing array.
  • Copy existing array into new array.
  • Set new element at end of new array.
fun main() {

	var arr = arrayOf<Int?>(1,2,3,4,5)

	arr = appendElement(arr, 10)

	println("Elements in Array: ")
	for(element in arr) {
		print(" $element")
	}
	println("")
}

fun appendElement(arr: Array<Int?>, newElement: Int): Array<Int?> {
	val array = arrayOfNulls<Int>(arr.size + 1)
	System.arraycopy(arr, 0, array, 0, arr.size)
	array[arr.size] = newElement
	return array
}

When you run the program, output will be –

Elements in Array:
1 2 3 4 5 10

3. Using Array.copyOf

You can use Array.copyOf to add new element in array in Kotlin. We can do it as below –

  • copyOf() function creates an array of given size and copy all the elements of given array.
  • Using copyOf() function, create new array and copy all the elements in it.
  • At last, set new element at end of array.
fun main() {

	var arr = arrayOf<Int?>(1,2,3,4,5)

	arr = appendElement(arr, 10)

	println("Elements in Array: ")
	for(element in arr) {
		print(" $element")
	}
	println("")
}

fun appendElement(arr: Array<Int?>, newElement: Int): Array<Int?> {
	val array = arr.copyOf(arr.size + 1)
	array[arr.size] = newElement
	return array
}

When you run the program, output will be –

Elements in Array:
1 2 3 4 5 10

All of the above solutions are workaround. If you know that at some point you are going to add some elements in array,
it’s better to use ArrayList.

4. Using ArrayList

We can use ArrayList to add element in array in Kotlin. We can do it as below –

fun main() {

	val arr = ArrayList<Int>()

	arr.add(1)
	arr.add(2)
	arr.add(10)

	println("Elements in Array: ")
	for(element in arr) {
		print(" $element")
	}
	println("")
}

When you run above program, output will be –

Elements in Array:
1 2 10

Using .add() function, we can add element in array.

Leave a Reply