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 pre 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: –
- Kotlin
Kotlin
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.
- Kotlin
Kotlin
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.
- Kotlin
Kotlin
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.
- Kotlin
Kotlin
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.
- Kotlin
Kotlin
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)
- Kotlin
Kotlin
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.
- Kotlin
Kotlin
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
In Kotlin, an ArrayList is a resizable list implementation backed by an array, similar to the ArrayList in Java. It allows dynamic resizing of the list, and provides various methods for adding, removing, and manipulating elements in the list.
To create an ArrayList in Kotlin, you can use the arrayListOf() function or the ArrayList constructor. For example:
- C#
C#
fun main() { val list = arrayListOf(1, 2, 3) println( "Initial list: $list" ) // Add elements to the list list.add(4) list.add(1, 5) // Adds 5 at index 1 println( "After adding elements: $list" ) // Remove elements from the list list.remove(2) list.removeAt(0) // Removes element at index 0 println( "After removing elements: $list" ) // Update elements in the list list[0] = 6 println( "After updating elements: $list" ) // Access elements in the list val first = list[0] val last = list.last() println( "First element: $first" ) println( "Last element: $last" ) // Iterate over the list for (element in list) { println(element) } } |
Output:
Initial list: [1, 2, 3]
After adding elements: [1, 5, 2, 3, 4]
After removing elements: [5, 3, 4]
After updating elements: [6, 3, 4]
First element: 6
Last element: 4
6
3
4
Here are some advantages and disadvantages of using an ArrayList in Kotlin:
Advantages:
Dynamic resizing: Unlike arrays, ArrayList can resize dynamically, which means you can add or remove elements from the list as needed.
Flexible storage: ArrayList can store objects of any type, making it useful for storing collections of heterogeneous data.
Easy manipulation: ArrayList provides a variety of methods for adding, removing, and manipulating elements in the list, making it easy to work with.
Disadvantages:
Overhead: ArrayList has a small amount of overhead compared to arrays, which can affect performance in certain situations.
Memory usage: ArrayList can use more memory than arrays because it stores each element as an object, which requires additional memory overhead.
Iteration performance: When iterating over an ArrayList, accessing elements by index can be slower than using an array, because each element is accessed through a function call rather than direct memory access.
Overall, ArrayList is a powerful and flexible collection type that is useful in many situations, but it may not always be the best choice depending on the specific requirements of your program.