Industrial Training




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


inline fun < T> arrayListOf(): ArrayList< T> (source)  
inline fun < T> arrayListOf(): ArrayList< T> (source) 

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

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.


fun main(args: Array< String>){  
     var arrayList = arrayListOf< Int>(4,7,12)  
      for(element in arrayList){  
        println(element)  
    }  
}  
fun main(args: Array< String>){  
     var arrayList = arrayListOf< Int>(4,7,12)  
      for(element in arrayList){  
        println(element)  
    }  
}  

Output:
4
7
12

Kotlin arrayListOf() Example 2


For more specific we can define the generic types of arrayListOf() function such as arrayListOf(), arrqayListOf(),arrayListOf(). Let's see the example.


fun main(args: Array< String>){  
  
    var intArrayList: ArrayList< Int> = arrayListOf< Int>(1,2,3)  
    var stringArrayList: ArrayList< String> = arrayListOf< String>("Ajay","Vijay","Prakash")  
    var anyArrayList: ArrayList< Any> = arrayListOf< Any>(1,2,3,"Ajay","Vijay","Prakash")  
    println("print int ArrayList")  
    for(element in intArrayList){  
        println(element)  
    }  
    println()  
    println("print string ArrayList")  
    for(element in stringArrayList){  
        println(element)  
    }  
        println()  
    println("print any ArrayList")  
    for(element in anyArrayList){  
        println(element)  
    }  
}  
fun main(args: Array< String>){  
  
    var intArrayList: ArrayList< Int> = arrayListOf< Int>(1,2,3)  
    var stringArrayList: ArrayList< String> = arrayListOf< String>("Ajay","Vijay","Prakash")  
    var anyArrayList: ArrayList< Any> = arrayListOf< Any>(1,2,3,"Ajay","Vijay","Prakash")  
    println("print int ArrayList")  
    for(element in intArrayList){  
        println(element)  
    }  
    println()  
    println("print string ArrayList")  
    for(element in stringArrayList){  
        println(element)  
    }  
        println()  
    println("print any ArrayList")  
    for(element in anyArrayList){  
        println(element)  
    }  
}  

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:


fun main(args: Array< String>){  
    val list: ArrayList< String> = arrayListOf< String>()  
  
    list.add("Ajay")  
    list.add("Vijay")  
    list.add("Prakash")  
  
    println(".......print ArrayList.......")  
    val itr = list.iterator()  
    while(itr.hasNext()) {  
        println(itr.next())  
    }  
}  
fun main(args: Array< String>){  
    val list: ArrayList< String> = arrayListOf< String>()  
  
    list.add("Ajay")  
    list.add("Vijay")  
    list.add("Prakash")  
  
    println(".......print ArrayList.......")  
    val itr = list.iterator()  
    while(itr.hasNext()) {  
        println(itr.next())  
    }  
} 

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:


fun main(args: Array< String>){  
  
    val list: ArrayList< String> = arrayListOf< String>()  
  
    list.add("Ajay")  
    list.add("Vijay")  
    list.add("Prakash")  
    list.add("Rohan")  
    list.add("Vijay")  
    println(".......print list.......")  
    for (i in list) {  
        println(i)  
    }  
    println(".......list.get(2).......")  
    println( list.get(2))  
}  
fun main(args: Array< String>){  
  
    val list: ArrayList< String> = arrayListOf< String>()  
  
    list.add("Ajay")  
    list.add("Vijay")  
    list.add("Prakash")  
    list.add("Rohan")  
    list.add("Vijay")  
    println(".......print list.......")  
    for (i in list) {  
        println(i)  
    }  
    println(".......list.get(2).......")  
    println( list.get(2))  
}  

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:


fun main(args: Array< String>){  
  
    val list: ArrayList< String> = arrayListOf< String>()  
  
    list.add("Ajay")  
    list.add("Vijay")  
    list.add("Prakash")  
  
    println(".......print list.......")  
    for (i in list) {  
        println(i)  
    }  
    println(".......arrayList.set(2,\"Rohan\").......")  
    list.set(2,"Rohan")  
    println(".......print ArrayList.......")  
    for (i in list) {  
        println(i)  
    }  
}  
fun main(args: Array< String>){  
  
    val list: ArrayList< String> = arrayListOf< String>()  
  
    list.add("Ajay")  
    list.add("Vijay")  
    list.add("Prakash")  
  
    println(".......print list.......")  
    for (i in list) {  
        println(i)  
    }  
    println(".......arrayList.set(2,\"Rohan\").......")  
    list.set(2,"Rohan")  
    println(".......print ArrayList.......")  
    for (i in list) {  
        println(i)  
    }  
}  

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.


class Employee(var id: Int, var name: String, var phone: Int, var city: String)  
class Employee(var id: Int, var name: String, var phone: Int, var city: String)  

fun main(args: Array< String>){  
    val arrayList: ArrayList< Employee> = arrayListOf< Employee>()  
    val e1 = Employee(101, "Ajay", 55555, "Delhi")  
    val e2 = Employee(102, "Rahul", 44443, "Mumbai")  
    val e3 = Employee(103, "Sanjay", 45422, "Noida")  
    arrayList.add(e1)  
    arrayList.add(e2)  
    arrayList.add(e3)  
  
    for (e in arrayList) {  
        println("${e.id} ${e.name} ${e.phone} ${e.city}")  
    }  
}  
fun main(args: Array< String>){  
    val arrayList: ArrayList< Employee> = arrayListOf< Employee>()  
    val e1 = Employee(101, "Ajay", 55555, "Delhi")  
    val e2 = Employee(102, "Rahul", 44443, "Mumbai")  
    val e3 = Employee(103, "Sanjay", 45422, "Noida")  
    arrayList.add(e1)  
    arrayList.add(e2)  
    arrayList.add(e3)  
  
    for (e in arrayList) {  
        println("${e.id} ${e.name} ${e.phone} ${e.city}")  
    }  
}  

Output:
101 Ajay 55555 Delhi
102 Rahul 44443 Mumbai
103 Sanjay 45422 Noida


Hi I am Pluto.