Kotlin ArrayList: arrayListOf()
An arrayListOf() is a function of ArrayList class. ArrayList is mutable which means it provides both read am write functionalities. The arrayListOf() function returns an ArrayList type.
Syntax of arrayListOf() function
Function of Kotlin ArrayList
Function | Description |
---|---|
open fun add(element: E): Boolean | It is used to add the specific element into the collection. |
open fun add(index: Int, element: E) | It is used to insert an element at specific index. |
open fun addAll(elements: Collection<E>): Boolean | It is used to add all the elements in the specified collection to current collection. |
open fun addAll(index: Int, elements: Collection<E>): Boolean | It is used to add all the elements of specified collection into the current list at the specified index. |
open fun clear() | It is used to removes all elements from the collection. |
open fun get(index: Int): E | It is used to return the element at specified index in the list. |
open fun indexOf(element: E): Int | It is used to return the index of first occurrence of specified element in the list or return -1 if the specified element in not present in the list. |
open fun lastIndexOf(element: E): Int | It is used to return the last occurrence of given element from the list or it returns -1 if the given element is not present in the list. |
open fun remove(element: E): Boolean | It is used to remove a single instance of the specific element from current collection, if it is available. |
open fun removeAt(index: Int): E | It is used to remove the specific index element from the list. |
open fun removeRange(startIndex: Int, endIndex: Int) | Its remove the range of elements starting from startIndex to endIndex in which endIndex is not includes. |
open fun set(index: Int, element: E): E | It is used to replaces the element from the specified position from current list with the specified element. |
open fun toArray(): Array<Any?> | It is used to return new array of type Array<Any?> with the elements of this collection. |
open fun toString(): String | It is used to returns a string representation of the object. |
fun trimToSize() | It does nothing in this ArrayList implementation. |
Kotlin arrayListOf() Example 1
Let’s create a simple example of arrayListOf() function.
Output:
4 7 12
Kotlin arrayListOf() Example 2
For more specific we can define the generic types of arrayListOf() function such as arrayListOf<Int>(), arrqayListOf<String>(),arrayListOf<Any>(). Let’s see the example.
Output:
print int ArrayList 1 2 3 print string ArrayList Ajay Vijay Prakash print any ArrayList 1 2 3 Ajay Vijay Prakash
Kotlin arrayListOf() Example 3- iterator() function
The elements of ArrayList class is also be traverse using inbuilt iterator() function. For example:
Output:
.......print ArrayList....... Ajay Vijay Prakash
Kotlin arrayListOf() Example 4 – get()
The get() function of arrayListOf() is used to retrieve the element present at specified index. For example:
Output:
.......print list....... Ajay Vijay Prakash Rohan Vijay .......list.get(2)....... Prakash
Kotlin arrayListOf() Example 5 – set()
The set() function of arrayListOf() is used to set the given element at specified index and replace if any element already present at that index. For example:
Output:
.......print list....... Ajay Vijay Prakash .......list.set(2,"Rohan")....... .......print list....... Ajay Vijay Rohan
Kotlin arrayListOf() Example – add and print Employee data
Let’s create an another example of arrayListOf() function of ArrayList class. In this example we are adding and traversing Employee class data. Here Employee class is bean class which defines the property of Employee.
Output:
101 Ajay 55555 Delhi 102 Rahul 44443 Mumbai 103 Sanjay 45422 Noida