Kotlin ArrayList: arrayListOf()An arrayListOf() is a function of ArrayList class. ArrayList is mutable which means it provides both read am write functionalities. The arrayListOf() function returns an ArrayList type. Syntax of arrayListOf() functionFunction of Kotlin ArrayList
Kotlin arrayListOf() Example 1Let's create a simple example of arrayListOf() function. Output: 4 7 12 Kotlin arrayListOf() Example 2For more specific we can define the generic types of arrayListOf() function such as arrayListOf<Int>(), arrqayListOf<String>(),arrayListOf<Any>(). Let's see the example. Output: print int ArrayList 1 2 3 print string ArrayList Ajay Vijay Prakash print any ArrayList 1 2 3 Ajay Vijay Prakash Kotlin arrayListOf() Example 3- iterator() functionThe elements of ArrayList class is also be traverse using inbuilt iterator() function. For example: Output: .......print ArrayList....... Ajay Vijay Prakash Kotlin arrayListOf() Example 4 - get()The get() function of arrayListOf() is used to retrieve the element present at specified index. For example: Output: .......print list....... Ajay Vijay Prakash Rohan Vijay .......list.get(2)....... Prakash Kotlin arrayListOf() Example 5 - set()The set() function of arrayListOf() is used to set the given element at specified index and replace if any element already present at that index. For example: Output: .......print list....... Ajay Vijay Prakash .......list.set(2,"Rohan")....... .......print list....... Ajay Vijay Rohan Kotlin arrayListOf() Example - add and print Employee dataLet's create an another example of arrayListOf() function of ArrayList class. In this example we are adding and traversing Employee class data. Here Employee class is bean class which defines the property of Employee. Output: 101 Ajay 55555 Delhi 102 Rahul 44443 Mumbai 103 Sanjay 45422 Noida Next TopicMap: mapOf() |