top of page

ArrayList Kotlin - Kotlin Assignment Help

Updated: Feb 15, 2023



Kotlin ArrayList class is used to create a dynamic array. This means the size of the ArrayList class can be increased or decreased according to requirement. The ArrayList class provides both read and write functionalities.


Kotlin ArrayList Example 1- empty ArrayList

fun main(args: Array<String>){  
  
    val arrayList = ArrayList<String>()//Creating an empty arraylist  
    arrayList.add("Ajay")//Adding object in arraylist  
    arrayList.add("Vijay")  
    arrayList.add("Prakash")  
    arrayList.add("Rohan")  
    arrayList.add("Vijay")  
    println(".......print ArrayList.......")  
    for (i in arrayList) {  
        println(i)  
    }  
}  


Output:

......print ArrayList...... 
Ajay 
Vijay 
Prakash 
Rohan 
Vijay 


Kotlin ArrayList Example 2- initialize ArrayList capacity

Let's create an ArrayList class with initialize its initial capacity. The capacity of the ArrayList class is not fixed and it can be changed later in the program according to requirement.

fun main(args: Array<String>){  
  
    val arrayList1 = ArrayList<String>(5)  
    arrayList1.add("Ajay")//Adding object in arraylist  
    arrayList1.add("Vijay")  
    arrayList1.add("Prakash")  
    arrayList1.add("Rohan")  
    arrayList1.add("Vijay")  
    println(".......print ArrayList1......")  
    for (i in arrayList1) {  
        println(i)  
    }  
    println("size of arrayList1 = "+arrayList1.size)  
    val arrayList2 = ArrayList<Int>(5)  
    arrayList2.add(14)  
    arrayList2.add(20)  
    arrayList2.add(80)  
    println("......print ArrayList2......")  
    for (i in arrayList2) {  
        println(i)  
    }  
    println("size of arrayList2 = "+arrayList2.size)  
}  

Output:


.......print ArrayList1...... 
Ajay
Vijay 
Prakash
Rohan
Vijay 
size of arrayList1 = 5
 ......print ArrayList2...... 
14
20
80
 size of arrayList2 = 3 


Kotlin ArrayList Example 3- filled elements in ArrayList using collection

The elements in Kotlin ArrayList class can also be added using other collections. More specifically in the ArrayList class, it is declared by its generic types. Elements of the


ArrayList class also be traversed using iterator() function. For example:

fun main(args: Array<String>){  
  
    val arrayList: ArrayList<String> = ArrayList<String>(5)  
    var list: MutableList<String> = mutableListOf<String>()  
  
    list.add("Ajay")  
    list.add("Vijay")  
    list.add("Prakash")  
  
    arrayList.addAll(list)  
    println("......print ArrayList......")  
    val itr = arrayList.iterator()  
    while(itr.hasNext()) {  
        println(itr.next())  
    }  
    println("size of arrayList = "+arrayList.size)  
}  

Output:

.......print ArrayList....... 
Ajay 
Vijay 
Prakash 
size of arrayList = 3 

Hope you understand the ArrayList and syntax of kotlin in the next blog we are going to learn set and hashset in kotlin.

Thank you


The journey of solving bug and completing project on time in Kotlin can be challenging and lonely. If you need help regarding other sides to Kotlin, we’re here for you!


Drop an email to us at contact@codersarts.com with the Project title, deadline, and requirement files. Our email team will revert back promptly to get started on the work.
bottom of page