Javatpoint Logo
Javatpoint Logo

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 It 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<Any?> with the elements of this collection.
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.

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.

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:

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:

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:

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:

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:

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:

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:

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:

Output:

.......print ArrayList.......
Ajay
Vijay
Prakash
Rohan
Vijay
.......arrayList.clear().......
.......arrayList.......
[]





Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


B.Tech / MCA