![]() |
VOOZH | about |
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.
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
Output :
true
ArrayList : []
Example 2: Create an ArrayList with String elements
Output :
false
ArrayList : [Java, Python, JavaScript]
Example 3: Create an ArrayList with mixed data types
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 :
Output :
0
1
The ArrayList class has the following functions:
1. add(element): This function is used to add the specified element to the ArrayList.
Example:
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:
Output :
[]
[GeeksforGeeks]
3. addAll(elementCollection): This function is used to add the specified collection of elements to the ArrayList.
Example:
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:
Output :
[Java, Kotlin]
[Java, Python, JavaScript, Kotlin]
5. clear(): This function is used to remove all the elements from the ArrayList.
Example:
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:
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:
Output :
true8. get(index): This function is used to retrieve an element at the specified index from the ArrayList
Example:
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:
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:
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:
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:
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:
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:
Output :
[Java, PHP, Kotlin, Python, Kotlin]15. toArray() This function is used to convert the ArrayList to an array of type Array.
Example:
Output :
Java
Kotlin
Python
Kotlin
16. toString() This function is used to get the string representation of the ArrayList object.
Example:
Output :
[Java, Kotlin, Python, Kotlin]17. isEmpty() This function returns true if the ArrayList is empty otherwise true
Example:
Output :
falseWe can use the following way to traverse through an ArrayList.
1. Using for loop with index:
Example:
Output :
0 => Java
1 => Python
2 => Kotlin
2. Using for-each loop (element wise):
Output :
Java
Python
Kotlin
3. Using while loop:
Output :
Java
Python
Kotlin
4. Using Iterator:
Output :
Java
Python
Kotlin