Left Index

Kotlin ArrayList

In Kotlin, ArrayList is used to create dynamic array i.e. size of array can be modify (increased or decreased) accordinly. ArrayList follows the sequence of insertion order. ArrayList class is non synchronized and it may contains duplicate elements. The elements of ArrayList class are accessed randomly as it works on index basis.


Example:
package com.theitbulls.kotlinex

import java.util.ArrayList

fun main(args: Array<String>) {
	// create ArrayList object using empty constructor
	var arrayList1 = ArrayList<String>();
	arrayList1.add("Tony")
	arrayList1.add("Wony")
	arrayList1.add("Honey")
	
	displayMsg(arrayList1)
	
	// create ArrayList with initial capacity
	var arrayList2 = ArrayList<String>(10);
	arrayList2.add("Tony")
	arrayList2.add("Wony")
	arrayList2.add("Honey")
	arrayList2.add("Vicky")
	arrayList2.add("Jack")
	arrayList2.add("Denial")
	
	println()
	displayMsg(arrayList2)
	
	// create ArrayList object using another Collection object
	var arrayList3 = ArrayList(arrayList1)
	arrayList3.add("Joney")
	
	println()
	displayMsg(arrayList3)
	
	var arrayList4 = ArrayList(listOf("Jay", "Rock"))
	println()
	println(arrayList4)
}

fun displayMsg(arrayList: ArrayList<String>) {
	arrayList.forEach { item -> println("Item: ${item}") }
}
Output:
Item: Tony
Item: Wony
Item: Honey

Item: Tony
Item: Wony
Item: Honey
Item: Vicky
Item: Jack
Item: Denial

Item: Tony
Item: Wony
Item: Honey
Item: Joney

[Jay, Rock]

You can find methods of Collection here. In above example you can see how to add data into ArrayList using add() method.


Remove Item from Kotlin ArrayList
Method Description
remove(String) Remove given item and return boolean(true or false)
remove(Int) Remove item at given index and return boolean (true or false)
removeAll() Remove all item from ArrayList
clear() Remove all item from ArrayList
Example:
package com.theitbulls.kotlinex

import java.util.ArrayList

fun main(args: Array<String>) {
	// create ArrayList object using empty constructor
	var arrayList = ArrayList<String>();
	arrayList.add("Tony")
	arrayList.add("Wony")
	arrayList.add("Honey")
	
	// remove item from arrayList
	println("Item removed: ${arrayList.remove("Wony")}")
	println("Item removed: ${arrayList.removeAt(1)}")
}
Output:
Item removed: true
Item removed: Honey

Get and Set Item to Kotlin ArrayList
Method Description
get(Int) Get item at given index
getOrElse(index, {i -> String}) Get item at given index, if not found the return given message
getOrNull(Int) Get Item at given index, if not found return null
set(Int, String) Set item at given index, iff ArrayList of String type
Example:
package com.theitbulls.kotlinex

import java.util.ArrayList

fun main(args: Array<String>) {
	// create ArrayList object using empty constructor
	var arrayList = ArrayList<String>();
	arrayList.add("Tony")
	arrayList.add("Wony")
	arrayList.add("Honey")
	
	println("Item at(4): " + arrayList.getOrElse(4, {i -> "Item not found at: ${i}"}))
	println("Item at(0): " + arrayList.getOrElse(0, {i -> "Item not found at: ${i}"}))

	arrayList.set(2, "Vijay")	
	
	println()
	println("Item at(4): " + arrayList.getOrNull(4));
	println("Item at(2): " + arrayList.getOrNull(2));
	
	println()
	println("Item at(2): ${arrayList.get(2)}")
	// below code will throw IndexOutOfBoundsException
	// to remove IndexOutOfBoundsException we use getOrElse() or getOrNull() method
	println("Item at(4): ${arrayList.get(4)}")
}
Output:
Item at(4): Item not found at: 4
Item at(0): Tony

Item at(3): null
Item at(0): Vijay

Item at(0): Tony
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 4, Size: 3
	at java.util.ArrayList.rangeCheck(ArrayList.java:657)
	at java.util.ArrayList.get(ArrayList.java:433)
	at com.theitbulls.kotlinex.HelloWorldKt.main(HelloWorld.kt:25)

Iterate ArrayList

We can iterate ArrayList using different-2 method, like using forEach loop, iterator() method etc...

Example:
package com.theitbulls.kotlinex

import java.util.ArrayList

fun main(args: Array<String>) {
	// create ArrayList object using empty constructor
	var arrayList = ArrayList<String>();
	arrayList.add("Tony")
	arrayList.add("Wony")
	arrayList.add("Honey")
	
	// using forEach loop	
	arrayList.forEach { item -> println("${item}") }
	println()
	
	// using forEachIndexed
	arrayList.forEachIndexed({index,  item -> println("Item at(${index}): ${item}") })
	println()
	
	// using iterator() method
	var iterate = arrayList.iterator()
	while(iterate.hasNext()) {
		println("${iterate.next()}")
	}
	println()
	
	// using for loop with index
	for(index in arrayList.indices) {
		println("Item at(${index}): ${arrayList.get(index)}")
	}
	println()
	
	// using forEach loop
	for(item in arrayList) {
		println("Item: ${item}")
	}
	println()
}
Output:
Tony
Wony
Honey

Item at(0): Tony
Item at(1): Wony
Item at(2): Honey

Tony
Wony
Honey

Item at(0): Tony
Item at(1): Wony
Item at(2): Honey

Item: Tony
Item: Wony
Item: Honey