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
fun main() {
val list = arrayListOf<String>()
println(list.isEmpty())
println("ArrayList : $list")
}
Output :
true
ArrayList : []
Example 2: Create an ArrayList with String elements
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
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 :
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:
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:
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:
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:
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:
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:
fun main() {
var arrList = arrayListOf<String>("Java", "Kotlin");
println(arrList.contains("Kotlin"));
}
Output :
true7. 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:
fun main() {
var arrList = arrayListOf<String>(
"Java", "Kotlin", "Python", "JavaScript");
var checkList = listOf("Python", "Java");
println(arrList.containsAll(checkList));
}
Output :
true8. get(index): This function is used to retrieve an element at the specified index from the ArrayList
Example:
fun main() {
var arrList = arrayListOf<String>(
"Java", "Kotlin", "Python", "JavaScript");
println(arrList.get(1));
}
Output :
Kotlin9. 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:
fun main() {
var arrList = arrayListOf<String>("Java", "Kotlin",
"Python", "Kotlin");
println(arrList.indexOf("Kotlin"));
}
Output :
110. 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:
fun main() {
var arrList = arrayListOf<String>("Java", "Kotlin", "Python", "Kotlin");
println(arrList.lastIndexOf("Kotlin"));
}
Output :
311. 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:
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:
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:
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:
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:
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:
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:
fun main() {
var arrList = arrayListOf<String>(
"Java", "Kotlin", "Python", "JavaScript");
println(arrList.isEmpty());
}
Output :
falseTraversing an ArrayList
We can use the following way to traverse through an ArrayList.
1. Using for loop with index:
Example:
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):
fun main() {
val list = arrayListOf("Java", "Python", "Kotlin")
for (element in list) {
println(element)
}
}
Output :
Java
Python
Kotlin
3. Using while loop:
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:
fun main() {
val list = arrayListOf("Java", "Python", "Kotlin")
val iterator = list.iterator()
while (iterator.hasNext()) {
println(iterator.next())
}
}
Output :
Java
Python
Kotlin