Theoretical Paper
- Computer Organization
- Data Structure
- Digital Electronics
- Object Oriented Programming
- Discrete Mathematics
- Graph Theory
- Operating Systems
- Software Engineering
- Computer Graphics
- Database Management System
- Operation Research
- Computer Networking
- Image Processing
- Internet Technologies
- Micro Processor
- E-Commerce & ERP
- Dart Programming
- Flutter Tutorial
- Numerical Methods Tutorials
- Flutter Tutorials
- Kotlin Tutorial
Practical Paper
Industrial Training
Kotlin ArrayList class
Kotlin ArrayList class is used to create a dynamic array. Which means the size of ArrayList class can be increased or decreased according to requirement. ArrayList class provides both read and write functionalities.
Kotlin ArrayList class follows the sequence of insertion order. ArrayList class is non synchronized and it may contains duplicate elements. The elements of ArrayList class are accessed randomly as it works on index basis.
Constructor of Kotlin ArrayList
Constructor | Description |
ArrayList< E>() | It is used to create an empty ArrayList |
ArrayList(capacity: Int) | It is used to create an ArrayList of specified capacity. |
ArrayList(elements: Collection< E>) | It is used to create an ArrayList filled from the elements of collection. |
Functions 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 | IIt 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 |
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 ArrayList Example 1- empty ArrayList
Let's create a simple example of ArrayList class define with empty ArrayList of String and add elements later.
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) } } 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 ArrayList class is not fixed and it can be change later in 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) } 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 ArratList class can also be added using other collection. For more specific in ArrayList class it is declared by its generic types. Elements of ArrayList class also be traverse 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) } 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
Kotlin ArrayList Example 4 - get()
The get() function of ArrayList class is used to retrieve the element present at given specified index. For example:
fun main(args: Array< String>){ val arrayList: ArrayList< String> = ArrayList< String>(5) arrayList.add("Ajay") arrayList.add("Vijay") arrayList.add("Prakash") arrayList.add("Rohan") arrayList.add("Vijay") println(".......print ArrayList.......") for (i in arrayList) { println(i) } println(".......arrayList.get(2).......") println( arrayList.get(2)) } fun main(args: Array< String>){ val arrayList: ArrayList< String> = ArrayList< String>(5) arrayList.add("Ajay") arrayList.add("Vijay") arrayList.add("Prakash") arrayList.add("Rohan") arrayList.add("Vijay") println(".......print ArrayList.......") for (i in arrayList) { println(i) } println(".......arrayList.get(2).......") println( arrayList.get(2)) }
Output:
.......print ArrayList....... Ajay Vijay Prakash Rohan Vijay .......arrayList.get(2)....... Prakash
Kotlin ArrayList Example 5 - set()
The set() function of ArrayList class is used to set the given element at specified index and replace if any element present at specified index. For example:
fun main(args: Array< String>){ val arrayList: ArrayList< String> = ArrayList< String>(5) arrayList.add("Ajay") arrayList.add("Vijay") arrayList.add("Prakash") arrayList.add("Rohan") arrayList.add("Vijay") println(".......print ArrayList.......") for (i in arrayList) { println(i) } println(".......arrayList.set(2,\"Ashu\").......") arrayList.set(2,"Ashu") println(".......print ArrayList.......") for (i in arrayList) { println(i) } } fun main(args: Array< String>){ val arrayList: ArrayList< String> = ArrayList< String>(5) arrayList.add("Ajay") arrayList.add("Vijay") arrayList.add("Prakash") arrayList.add("Rohan") arrayList.add("Vijay") println(".......print ArrayList.......") for (i in arrayList) { println(i) } println(".......arrayList.set(2,\"Ashu\").......") arrayList.set(2,"Ashu") println(".......print ArrayList.......") for (i in arrayList) { println(i) } }
Output:
.......print ArrayList....... Ajay Vijay Prakash Rohan Vijay .......arrayList.set(2,"Ashu")....... .......print ArrayList....... Ajay Vijay Ashu Rohan Vijay
Kotlin ArrayList Example 6 - indexOf()
The indexOf() function of ArrayList class is used to retrieve the index value of first occurrence of element or return -1 if the specified element in not present in the list. For example:
fun main(args: Array< String>){ val arrayList: ArrayList< String> = ArrayList< String>(5) arrayList.add("Ajay") arrayList.add("Vijay") arrayList.add("Prakash") arrayList.add("Rohan") arrayList.add("Vijay") println(".......print ArrayList.......") for (i in arrayList) { println(i) } println(".......arrayList.indexOf(\"Vijay\").......") println(arrayList.indexOf("Vijay")) } fun main(args: Array< String>){ val arrayList: ArrayList< String> = ArrayList< String>(5) arrayList.add("Ajay") arrayList.add("Vijay") arrayList.add("Prakash") arrayList.add("Rohan") arrayList.add("Vijay") println(".......print ArrayList.......") for (i in arrayList) { println(i) } println(".......arrayList.indexOf(\"Vijay\").......") println(arrayList.indexOf("Vijay")) }
Output:
.......print ArrayList....... Ajay Vijay Prakash Rohan Vijay .......arrayList.indexOf("Vijay")....... 1
Kotlin ArrayList Example 7 - lastIndexOf()
The lastindexOf() function of ArrayList class is used to retrieve the index value of last occurrence of element or return -1 if the specified element in not present in the list. For example:
fun main(args: Array< String>){ val arrayList: ArrayList< String> = ArrayList< String>(5) arrayList.add("Ajay") arrayList.add("Vijay") arrayList.add("Prakash") arrayList.add("Rohan") arrayList.add("Vijay") println(".......print ArrayList.......") for (i in arrayList) { println(i) } println(".......arrayList.lastIndexOf(\"Vijay\").......") println(arrayList.lastIndexOf("Vijay")) } fun main(args: Array< String>){ val arrayList: ArrayList< String> = ArrayList< String>(5) arrayList.add("Ajay") arrayList.add("Vijay") arrayList.add("Prakash") arrayList.add("Rohan") arrayList.add("Vijay") println(".......print ArrayList.......") for (i in arrayList) { println(i) } println(".......arrayList.lastIndexOf(\"Vijay\").......") println(arrayList.lastIndexOf("Vijay")) }
Output:
.......print ArrayList....... Ajay Vijay Prakash Rohan Vijay .......arrayList.lastIndexOf("Vijay")....... 4
Kotlin ArrayList Example 8 - remove()
The remove () function of ArrayList class is used to remove the first occurrence of element if it is present in the list. For example:
fun main(args: Array< String>){ val arrayList: ArrayList< String> = ArrayList< String>(5) arrayList.add("Ajay") arrayList.add("Vijay") arrayList.add("Prakash") arrayList.add("Rohan") arrayList.add("Vijay") println(".......print ArrayList.......") for (i in arrayList) { println(i) } println(".......arrayList.remove(\"Vijay\").......") arrayList.remove("Vijay") for (i in arrayList) { println(i) } } fun main(args: Array< String>){ val arrayList: ArrayList< String> = ArrayList< String>(5) arrayList.add("Ajay") arrayList.add("Vijay") arrayList.add("Prakash") arrayList.add("Rohan") arrayList.add("Vijay") println(".......print ArrayList.......") for (i in arrayList) { println(i) } println(".......arrayList.remove(\"Vijay\").......") arrayList.remove("Vijay") for (i in arrayList) { println(i) } }
Output:
.......print ArrayList....... Ajay Vijay Prakash Rohan Vijay .......arrayList.remove("Vijay")....... Ajay Prakash Rohan Vijay
Kotlin ArrayList Example 9 - removeAt()
The removeAt() function of ArrayList class is used to remove the element of specified index from the list. For example:
fun main(args: Array< String>){ val arrayList: ArrayList< String> = ArrayList< String>(5) arrayList.add("Ajay") arrayList.add("Vijay") arrayList.add("Prakash") arrayList.add("Rohan") arrayList.add("Vijay") println(".......print ArrayList.......") for (i in arrayList) { println(i) } println(".......arrayList.remove(3).......") arrayList.removeAt(3) for (i in arrayList) { println(i) } } fun main(args: Array< String>){ val arrayList: ArrayList< String> = ArrayList< String>(5) arrayList.add("Ajay") arrayList.add("Vijay") arrayList.add("Prakash") arrayList.add("Rohan") arrayList.add("Vijay") println(".......print ArrayList.......") for (i in arrayList) { println(i) } println(".......arrayList.remove(3).......") arrayList.removeAt(3) for (i in arrayList) { println(i) } }
Output:
.......print ArrayList....... Ajay Vijay Prakash Rohan Vijay .......arrayList.remove(3)....... Ajay Vijay Prakash Vijay
Kotlin ArrayList Example 10 - clear()
The clear() function of ArrayList class is used to remove (clear) all the elements of list. For example:
fun main(args: Array< String>){ val arrayList: ArrayList< String> = ArrayList< String>(5) arrayList.add("Ajay") arrayList.add("Vijay") arrayList.add("Prakash") arrayList.add("Rohan") arrayList.add("Vijay") println(".......print ArrayList.......") for (i in arrayList) { println(i) } println(".......arrayList.clear().......") arrayList.clear() for (i in arrayList) { println(i) } println(".......arrayList.......") println(arrayList) } fun main(args: Array< String>){ val arrayList: ArrayList< String> = ArrayList< String>(5) arrayList.add("Ajay") arrayList.add("Vijay") arrayList.add("Prakash") arrayList.add("Rohan") arrayList.add("Vijay") println(".......print ArrayList.......") for (i in arrayList) { println(i) } println(".......arrayList.clear().......") arrayList.clear() for (i in arrayList) { println(i) } println(".......arrayList.......") println(arrayList) }
Output:
.......print ArrayList....... Ajay Vijay Prakash Rohan Vijay .......arrayList.clear()....... .......arrayList....... []