Kotlin arrayListOf()

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.



filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args : Array<String>) {
    var arrList = arrayListOf<String>()
    println(arrList.isEmpty())
    println("ArrayList : ${arrList}")
}

chevron_right


Output :

true
ArrayList : []

Example 2: Kotlin program to make new ArrayList with String elements

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args : Array<String>) {
    var arrList = arrayListOf<String>("Java", "Python", "JavaScript")
    println(arrList.isEmpty())
    println("ArrayList : ${arrList}")
}

chevron_right


Output :

false
ArrayList : [Java, Python, JavaScript]

Example 3: Kotlin program to make new ArrayList with elements of any data type

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args : Array<String>) {
    var arrList = arrayListOf<Any>(1, 2, 3, "GeeksforGeeks", 100.0)
    println(arrList.isEmpty())
    println("ArrayList : ${arrList}")
}

chevron_right


Output :

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

Property

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

Example :

filter_none

edit
close

play_arrow

link
brightness_4
code

fun main(args : Array<String>) {
    var arrList = arrayListOf<String>();
    println(arrList.size);
    arrList.add("GeeksforGeeks");
    println(arrList.size);
}

chevron_right


Output :



0
1

Functions

The ArrayList class has following functions :

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

[]
[GeeksforGeeks]

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

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

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



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

true

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

Kotlin
[Java, Kotlin, Python]

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

[Java, PHP, Kotlin, Python, Kotlin]

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



filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

Java
Kotlin
Python
Kotlin

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

[Java, Kotlin, Python, Kotlin]

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

false

Traversal of the ArrayList

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

Using for loop (index wise)

filter_none

edit
close

play_arrow

link
brightness_4
code

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)}");
    }
}

chevron_right


Output :

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

Using for loop (element wise)

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

Java
Python
JavaScript
Kotlin

Using while loop

filter_none

edit
close

play_arrow

link
brightness_4
code

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++;
    }
}

chevron_right


Output :

Java
Python
JavaScript
Kotlin

Using Iterator

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output :

Java
Python
JavaScript
Kotlin



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : ManasChhabra2

Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.