Open In App

Kotlin arrayListOf()

Last Updated : 12 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Kotlin, the arrayListOf() function is used to create a new ArrayList. An ArrayList is a mutable collection, which means that we can add, remove, or update its elements after it is created.

Syntax:

1. To create an empty ArrayList:

fun arrayListOf(): ArrayList<T>

This creates an empty ArrayList that doesn’t contain any elements.

2. To create an ArrayList with some initial elements:

fun arrayListOf(vararg elements: T): ArrayList<T>

This creates a new ArrayList with the given elements.

Example 1: Create an empty ArrayList

Kotlin
fun main() {
    val list = arrayListOf<String>()
    println(list.isEmpty())
    println("ArrayList : $list")
}

Output :

true
ArrayList : []


Example 2: Create an ArrayList with String elements

Kotlin
fun main() {
    val list = arrayListOf("Java", "Python", "JavaScript")
    println(list.isEmpty())
    println("ArrayList : $list")
}

Output :

false
ArrayList : [Java, Python, JavaScript]


Example 3: Create an ArrayList with mixed data types

Kotlin
fun main() {
    val list = arrayListOf(1, 2, 3, "GeeksforGeeks", 100.0)
    println(list.isEmpty())
    println("ArrayList : $list")
}

Output :

false
ArrayList : [1, 2, 3, GeeksforGeeks, 100.0]


Property of ArrayList

ArrayList in kotlin has one property i.e. size. It returns the number of element in the ArrayList. 

Example : 

Kotlin
fun main() {
    val list = arrayListOf<String>()
    println(list.size)
    
    list.add("Kotlin")
    println(list.size)
}

Output :

0
1


Common Functions of ArrayList

The ArrayList class has the following functions: 

1. add(element): This function is used to add the specified element to the ArrayList. 

Example:

Kotlin
fun main() {
    var arrList = arrayListOf<String>("Java", "Python");
    println(arrList);
    arrList.add(0, "Kotlin");
    println(arrList);
}

Output :

[Java, Python]
[Kotlin, Java, Python]


2. add(index, element): This function is used to add the element to the provided index of ArrayList. 

Example:

Kotlin
fun main() {
    var arrList = arrayListOf<String>();
    println(arrList);
    arrList.add("GeeksforGeeks");
    println(arrList);
}

Output :

[]
[GeeksforGeeks]


3. addAll(elementCollection): This function is used to add the specified collection of elements to the ArrayList. 

Example:

Kotlin
fun main() {
    var arrList = arrayListOf<String>("Java", "Kotlin");
    println(arrList);
    arrList.addAll(listOf("Python", "JavaScript"));
    println(arrList);
}

Output :

[Java, Kotlin]
[Java, Kotlin, Python, JavaScript]


4. addAll(index, elementCollection): This function is used to add the specified collection of elements to the ArrayList at provided index. 

Example:

Kotlin
fun main() {
    var arrList = arrayListOf<String>("Java", "Kotlin");
    println(arrList);
    arrList.addAll(1, listOf("Python", "JavaScript"));
    println(arrList);
}

Output :

[Java, Kotlin]
[Java, Python, JavaScript, Kotlin]


5. clear(): This function is used to remove all the elements from the ArrayList. 

Example:

Kotlin
 fun main() {
    var arrList = arrayListOf<String>("Java", "Kotlin");
    println(arrList);
    arrList.clear();
    println(arrList);
}

Output :

[Java, Kotlin]
[]


6. contains(element): This function is used to check whether an element exists in the ArrayList. It returns either true, if found, otherwise false 

Example:

Kotlin
fun main() {
    var arrList = arrayListOf<String>("Java", "Kotlin");
    println(arrList.contains("Kotlin"));
}

Output :

true


7. 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 

Example:

Kotlin
fun main() {
    var arrList = arrayListOf<String>(
        "Java", "Kotlin", "Python", "JavaScript");
    var checkList = listOf("Python", "Java");
    println(arrList.containsAll(checkList));
}

Output :

true


8. get(index): This function is used to retrieve an element at the specified index from the ArrayList 

Example:

Kotlin
fun main() {
    var arrList = arrayListOf<String>(
        "Java", "Kotlin", "Python", "JavaScript");
    println(arrList.get(1));
}

Output :

Kotlin


9. indexOf(element): This function returns the index of the first occurrence of the specified element in the ArrayList. If element is not present in the ArrayList, then it returns -1.

Example:

Kotlin
fun main() {
    var arrList = arrayListOf<String>("Java", "Kotlin",
                                      "Python", "Kotlin");
    println(arrList.indexOf("Kotlin"));
}

Output :

1


10. lastIndexOf(element): This function returns the index of the last occurrence of the specified element in the ArrayList. If the element is not present in the ArrayList, then it returns -1.

Example:

Kotlin
fun main() {
    var arrList = arrayListOf<String>("Java", "Kotlin", "Python", "Kotlin");
    println(arrList.lastIndexOf("Kotlin"));
}

Output :

3


11. 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. 

Example:

Kotlin
fun main() {
    var arrList = arrayListOf<String>("Java", "Kotlin",
                                      "Python", "Kotlin");
    println(arrList.remove("Kotlin"));
    println(arrList);
}

Output :

true
[Java, Python, Kotlin]


12. 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. 

Example:

Kotlin
fun main() {
    var arrList = arrayListOf<String>("Java", "Kotlin",
                                      "Python", "Kotlin");
    var delList = listOf("Java", "Kotlin");
    println(arrList.removeAll(delList));
    println(arrList);
}

Output :

true
[Python]


13. 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. 

Example:

Kotlin
fun main() {
    var arrList = arrayListOf<String>("Java", "Kotlin",
                                      "Python", "Kotlin");
    println(arrList.removeAt(3));
    println(arrList);
}

Output :

Kotlin
[Java, Kotlin, Python]


14. set(index, element) This function is used to add an element to the specified position in the ArrayList. 

Example:

Kotlin
fun main() {
    var arrList = arrayListOf<String>("Java", "Kotlin",
                                      "Python", "Kotlin");
    arrList.add(1, "PHP");
    println(arrList);
}

Output :

[Java, PHP, Kotlin, Python, Kotlin]


15. toArray() This function is used to convert the ArrayList to an array of type Array

Example:

Kotlin
fun main() {
    var arrList = arrayListOf<String>("Java", "Kotlin",
                                      "Python", "Kotlin");
    var arr = arrList.toArray();
    for (i in arr) {
        println(i);
    }
}

Output :

Java
Kotlin
Python
Kotlin


16. toString() This function is used to get the string representation of the ArrayList object. 

Example:

Kotlin
fun main(args: Array<String>) {
    var arrList = arrayListOf<String>("Java", "Kotlin",
                                      "Python", "Kotlin")
    var arr = arrList.toString()
    println(arr)
}

Output :

[Java, Kotlin, Python, Kotlin]


17. isEmpty() This function returns true if the ArrayList is empty otherwise true 

Example:

Kotlin
fun main() {
    var arrList = arrayListOf<String>(
        "Java", "Kotlin", "Python", "JavaScript");
    println(arrList.isEmpty());
}

Output :

false


Traversing an ArrayList

We can use the following way to traverse through an ArrayList.

1. Using for loop with index:

Example:

Kotlin
fun main() {
    val list = arrayListOf("Java", "Python", "Kotlin")
    for (i in list.indices) {
        println("$i => ${list[i]}")
    }
}

Output :

0 => Java
1 => Python
2 => Kotlin

2. Using for-each loop (element wise):

Kotlin
fun main() {
    val list = arrayListOf("Java", "Python", "Kotlin")
    for (element in list) {
        println(element)
    }
}

Output :

Java
Python
Kotlin

3. Using while loop: 

Kotlin
fun main() {
    val list = arrayListOf("Java", "Python", "Kotlin")
    var index = 0
    while (index < list.size) {
        println(list[index])
        index++
    }
}

Output :

Java
Python
Kotlin

4. Using Iterator:

Kotlin
fun main() {
    val list = arrayListOf("Java", "Python", "Kotlin")
    val iterator = list.iterator()
    while (iterator.hasNext()) {
        println(iterator.next())
    }
}

Output :

Java
Python
Kotlin

Article Tags :

Similar Reads