ArrayList in kotlin example
Kotlin ArrayList class is used to create a dynamic array. This means the size of the ArrayList class can be increased or decreased according to the requirement. The arrayList class provides both read and write functionalities.
Kotlin ArrayList class follows the sequence of the insertion order. ArrayList class is non-synchronized and may contain duplicate elements. The elements of the ArrayList class are accessed randomly as it works on an index basis.
example arraylist kotlin:
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)
}
}
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 provides read and writes 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 a string or another array, and many more functionalities.
example arraylist 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)
}
No comments:
Post a Comment