Kotlin arrayListOf()

26 Nov, 2019
The arrayList() is function of Kotlin ArrayList class, which is used to create a new ArrayList. ArrayList is mutable which means that we can modify the content of ArrayList.
Syntax:
fun  arrayListOf()

It is used to create an empty new ArrayList.
fun  arrayListOf(vararg elements: T)

It is used to create a new ArrayList with provided elements.
Example 1: Kotlin program to make new empty ArrayList.
fun main(args : Array<String>) {    var arrList = arrayListOf<String>()    println(arrList.isEmpty())    println("ArrayList : ${arrList}")}
Output :
true
ArrayList : []

Example 2: Kotlin program to make new ArrayList with String elements
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Python", "JavaScript")    println(arrList.isEmpty())    println("ArrayList : ${arrList}")}
Output :
false
ArrayList : [Java, Python, JavaScript]

Example 3: Kotlin program to make new ArrayList with elements of any data type
fun main(args : Array<String>) {    var arrList = arrayListOf<Any>(1, 2, 3, "GeeksforGeeks", 100.0)    println(arrList.isEmpty())    println("ArrayList : ${arrList}")}
Output :
false
ArrayList : [1, 2, 3, GeeksforGeeks, 100.0]

ArrayList in kotlin has one property i.e. size. It returns the number of element in the ArrayList.
Example :
fun main(args : Array<String>) {    var arrList = arrayListOf<String>();    println(arrList.size);    arrList.add("GeeksforGeeks");    println(arrList.size);}
Output :
0
1

The ArrayList class has following functions :
add(element): This function is used to add the specified element to the ArrayList.
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Python");    println(arrList);    arrList.add(0, "Kotlin");    println(arrList);    }
Output :
[]
[GeeksforGeeks]

add(index, element): This function is used to add the element to the provided index of ArrayList.
fun main(args : Array<String>) {    var arrList = arrayListOf<String>();    println(arrList);    arrList.add("GeeksforGeeks");    println(arrList);    }
Output :
[Java, Python]
[Kotlin, Java, Python]

addAll(elementCollection): This function is used to add the specified collection of elements to the ArrayList.
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Kotlin");    println(arrList);        arrList.addAll(listOf("Python", "JavaScript"));    println(arrList);}
Output :
[Java, Kotlin]
[Java, Kotlin, Python, JavaScript]

addAll(index, elementCollection): This function is used to add the specified collection of elements to the ArrayList at provided index.
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Kotlin");    println(arrList);        arrList.addAll(1, listOf("Python", "JavaScript"));    println(arrList);}
Output :
[Java, Kotlin]
[Java, Python, JavaScript, Kotlin]

clear(): This function is used to remove all the elements from the ArrayList.
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Kotlin");    println(arrList);        arrList.clear();    println(arrList);}
Output :
[Java, Kotlin]
[]

contains(element): This function is used to check whether an element exists in the ArrayList. It returns either true, if found, otherwise false
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Kotlin");    println(arrList.contains("Kotlin"));}
Output :
true

containsAll(elementCollection): This function is used to check whether a collection of elements exists in the ArrayList. It returns either true, if found, otherwise false
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Kotlin", "Python", "JavaScript");    var checkList = listOf("Python", "Java");    println(arrList.containsAll(checkList));}
Output :
true

get(index): This function is used to retrieve an element at the specified index from the ArrayList
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Kotlin", "Python", "JavaScript");    println(arrList.get(1));}
Output :
Kotlin

indexOf(element) This function returns the index of first occurrence of the specified element in the ArrayList. If element is not present in the ArrayList, then it returns -1
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Kotlin", "Python", "Kotlin");    println(arrList.indexOf("Kotlin"));}
Output :
1

lastIndexOf(element) This function returns the index of last occurrence of the specified element in the ArrayList. If element is not present in the ArrayList, then it returns -1
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Kotlin", "Python", "Kotlin");    println(arrList.lastIndexOf("Kotlin"));}
Output :
3

remove(element) This function is used to remove a single instance of the specified element from the ArrayList. It returns true, if the element was present in the ArrayList and is removed, otherwise false
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Kotlin", "Python", "Kotlin");    println(arrList.remove("Kotlin"));    println(arrList);}
Output :
true
[Java, Python, Kotlin]

removeAll(elementCollection) This function is used to remove collection of element from the ArrayList. It returns true, if the element collection was removed, otherwise false
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Kotlin", "Python", "Kotlin");    var delList = listOf("Java", "Kotlin");    println(arrList.removeAll(delList));    println(arrList);}
Output :
true
[Python]

removeAt(index): This function is used to remove an element by its position in the ArrayList. It returns true, if the element collection was removed, otherwise false
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Kotlin", "Python", "Kotlin");    println(arrList.removeAt(3));    println(arrList);}
Output :
Kotlin
[Java, Kotlin, Python]

set(index, element) This function is used to add an element to the specified position in the ArrayList.
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Kotlin", "Python", "Kotlin");    arrList.add(1, "PHP");    println(arrList);}
Output :
[Java, PHP, Kotlin, Python, Kotlin]

toArray() This function is used to convert the ArrayList to an array of type Array.
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Kotlin", "Python", "Kotlin");    var arr = arrList.toArray();    for (i in arr) {        println(i);    }}
Output :
Java
Kotlin
Python
Kotlin

toString() This function is used to get the string representation of the ArrayList object.
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Kotlin", "Python", "Kotlin");    var arr = arrList.toString();    println(arr);}
Output :
[Java, Kotlin, Python, Kotlin]

isEmpty() This function returns true if the ArrayList is empty otherwise true
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Kotlin", "Python", "JavaScript");    println(arrList.isEmpty());}
Output :
false

We can use the following way to traverse through an ArrayList.
Using for loop (index wise)
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Python", "JavaScript", "Kotlin");    for (index in 0..arrList.size-1) {        println("${index} => ${arrList.get(index)}");    }}
Output :
0 => Java
1 => Python
2 => JavaScript
3 => Kotlin

Using for loop (element wise)
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Python", "JavaScript", "Kotlin");    for (value in arrList) {        println(value);    }}
Output :
Java
Python
JavaScript
Kotlin

Using while loop
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Python", "JavaScript", "Kotlin");    var i = 0;    while (i < arrList.size) {        println(arrList.get(i))        i++;    }}
Output :
Java
Python
JavaScript
Kotlin

Using Iterator
fun main(args : Array<String>) {    var arrList = arrayListOf<String>("Java", "Python", "JavaScript", "Kotlin");    var itr = arrList.iterator();    while (itr.hasNext()) {        println(itr.next());    }}
Output :
Java
Python
JavaScript
Kotlin

ManasChhabra2
Kotlin Collections
Picked
Kotlin
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Add Views Dynamically and Store Data in Arraylist in Android?
How to Communicate Between Fragments in Android?
Retrofit with Kotlin Coroutine in Android
Suspend Function In Kotlin Coroutines
How to Get Current Location in Android?
Kotlin extension function
Dagger Hilt in Android with Example
Kotlin Sealed Classes
Scopes in Kotlin Coroutines
Singleton Class in Kotlin