Kotlin ArrayList classKotlin 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
Functions of Kotlin ArrayList
Kotlin ArrayList Example 1- empty ArrayListLet'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 capacityLet'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 collectionThe 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....... []
Next TopicArrayList: arrayListOf()
|