Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Kotlin list : Arraylist

  • Last Updated : 07 Aug, 2019

ArrayList class is used to create a dynamic array in Kotlin. Dynamic array states that we can increase or decrease the size of an array as per requisites. It also provide read and write functionalities. ArrayList may contain duplicates and is non-synchronized in nature. We use ArrayList to access the index of the specified element, convert an Arraylist into string or another array and many more functionalities.

Constructors –

1) ArrayList<E>(): – Creates an empty ArrayList
2) ArrayList(capacity: Int): – Creates an ArrayList of specified size.
3) ArrayList(elements: Collection<E>): – Create an ArrayList filled by collection elements.

Some of the important Methods –

add(index:Int, element: E): Boolean

It is used to add the specific element into the ArrayList. The 2nd parameter contains the element to be added which is the mandatory one and the 1st one is the index to which we want to add the element, it is optional and by default the value of it is 1 + last index of array.
Example: –




fun main(args: Array<String>) {
    // creating empty arraylist using constructor
    var arraylist = ArrayList<String>()
    //adding String elements in the list
    arraylist.add("Geeks")
  
    arraylist.add("Geeks")
    // iterating the list
    println("Array list ---->")
    for(i in arraylist)
        println(i)
  
    arraylist.add( 1 , "For")
    println("Arraylist after insertion ---->")
    for(i in arraylist)
        println(i)
}

Output:

Arraylist ---->
Geeks
Geeks
Arraylist after insertion ---->
Geeks
For
Geeks

addAll(index: Int, elements: Collection): Boolean

It is used to add all the elements of specified collection into the current list at the specified index. 1st parameter is the index value which is again optional one.






fun main(args: Array<String>) {
    // creating empty arraylist using constructor
    var arraylist=ArrayList<String>()
    //adding String elements in the list
    arraylist.add("Geeks")
    arraylist.add("For")
    arraylist.add("Geeks")
    // creating new arraylist1
    var arraylist1=ArrayList<String>()
    //adding all elements from arraylist to arraylist1
    println("Elements in arraylist1 -->")
    arraylist1.addAll(arraylist)
    for(i in arraylist1)
        println(i)
}

Output:

Elements in arraylist1 -->
Geeks
For
Geeks

get(index: Int): E

It is used to return the element at specified index in the list.




fun main(args: Array<String>) {
    // creating empty arraylist using constructor
    var arraylist=ArrayList<Int>()
    // adding elements
    arraylist.add(10)
    arraylist.add(20)
    arraylist.add(30)
    arraylist.add(40)
    arraylist.add(50)
  
    // iterating through the elements
    for(i in arraylist)
    print("$i ")
    println()
    println("Accessing the index 2 of arraylist: "+arraylist.get(3))
}

Output:

10 20 30 40 50
Accessing the index 2 of arraylist: 40

set(index: Int, element: E):E

It is used to replaces the element from the specified position from current list with the specified element passed as arguments.




fun main(args: Array<String>) {
    // creating empty arraylist using constructor
    var arraylist=ArrayList<String>()
    // adding elements
    arraylist.add("Geeks")
    arraylist.add("for")
    arraylist.add("Geeks:")
    arraylist.add("Portal")
    // iterating through the elements
    for(i in arraylist)
        print("$i ")
    println()
    // set the element at index 3 with new string
    arraylist.set(3,"A computer Science portal for students")
    // iterating through the elements
    for(i in arraylist)
        print("$i ")
}

Output:

Geeks for Geeks: Portal 
Geeks for Geeks: A computer Science portal for students 

indexOf(element: E): Int

It is used to return the index of first occurrence of specified element in the list and it return -1 if the specified element in not present in the list.




fun main(args: Array<String>) {
    // creating empty arraylist using constructor
    var arraylist=ArrayList<String>()
    // adding elements
    arraylist.add("Geeks")
    arraylist.add("for")
    arraylist.add("Geeks")
  
    // iterating through the elements
    for(i in arraylist)
        print("$i ")
    println()
    println("The index of the element is: "+arraylist.indexOf("Geeks"))
}

Output:

Geeks for Geeks 
The index of the element is: 0

remove(element: E): Boolean

It is used to remove the first occurrence of the specific element from current collection, if it is available. Similarly for removing element at index i we use removeAt(index)




fun main(args: Array<String>) {
    // creating empty arraylist using constructor
    var arraylist=ArrayList<String>()
    // adding elements
    arraylist.add("Geeks")
    arraylist.add("for")
    arraylist.add("Geeks")
  
    arraylist.remove("for")
    // iterating through the elements
    for(i in arraylist)
        print("$i ")
}

Output:

Geeks Geeks

clear()

It is used to remove all the elements from the list.




fun main(args: Array<String>) {
    // creating empty arraylist using constructor
    var arraylist=ArrayList<Int>()
    // adding elements
    arraylist.add(10)
    arraylist.add(20)
    arraylist.add(30)
    arraylist.add(40)
    arraylist.add(50)
  
    // iterating through the elements
    for(i in arraylist)
        print("$i ")
    arraylist.clear()
    println()
    println("The size of arraylist after clearing all elements: "+arraylist.size)
}

Output:

10 20 30 40 50 
The size of arraylist after clearing all the elements: 0



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!