Kotlin list : Arraylist

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: –



filter_none

edit
close

play_arrow

link
brightness_4
code

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)
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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)
}

chevron_right


Output:

Elements in arraylist1 -->
Geeks
For
Geeks

get(index: Int): E

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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))
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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 ")
}

chevron_right


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.

filter_none

edit
close

play_arrow

link
brightness_4
code

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"))
}

chevron_right


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)

filter_none

edit
close

play_arrow

link
brightness_4
code

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 ")
}

chevron_right


Output:

Geeks Geeks

clear()

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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)
}

chevron_right


Output:

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


My Personal Notes arrow_drop_up

Im a final year MCA student at Panjab University, Chandigarh, one of the most prestigious university of India I am skilled in various aspects related to Web Development and AI I have worked as a freelancer at upwork and thus have knowledge on various aspects related to NLP, image processing and web

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.