Updated March 31, 2023
Introduction to Kotlin arrayList
The arrayList is defined as one of the kotlin collection that can be used to create the dynamic array the user may create additionally or removed the data at run time it automatically add or delete the datas in the list based on the requirement so based on this scenario the arrayList class provides both read and write access for the functions, and also it follows some sequential order for to insert the values on the memory the values are stored as the separate index with the help of indexes the values are retrieved from back end to frontend.
Syntax of Kotlin arrayList
The kotlin language has many default classes, interfaces, and methods to implement the code collections. Based on the requirement, it may varies the data either may be stored or removed from both user and programmer end.
fun main(args:Array<String>)
{
val vars=ArrayList<String>(size)
vars.add("") // add() is the default method
vars.remove(index value) // remove is the default method
----some logic codes---
}
The above codes are the basic syntax for to utilise the arrayList() class in the application. However, based on the programming and user requirements, we may use additional in-built methods for to accessing and implement the application.
How arrayList Works in Kotlin?
The arrayList is one of the collection class in the kotlin language; it is mainly used for to store the datas in a dynamic format. Mainly the size of the ArrayList class can be increased or decreased according to the specific requirement. The ArrayList class supports some default built-in methods like add(), remove() etc. It also provides both read and write operations in all the default functions. Each set of collections with default memory size will be allocated like that ArrayList class is initialized with the initial amount of capacity that is not fixable, and it can be changed later in the programming requirement.
Element or value is the type which is used in ArrayList class on the kotlin language, and also it can be added using the other collection the class also declared by using its generic types. The element in the ArrayList class can be traversed using the iterator() method based on the looping statement. Each value is iterated and the element present at the specified index value. The duplicate items/elements are accepted on the arrayList class; the index must be unique to retrieve the elements from the database. Thus, we can able to create the arrayList implementation with the mutable list.
Examples of Kotlin arrayList
Given below are the examples of Kotlin arrayList:
Example #1
Code:
package one;
class Test {
fun first(){
val m=41
println(m)
var aLs=ArrayList<String>()
aLs.add("Your first input is entered on the input text box")
aLs.add("Your second input is entered on the input text box")
aLs.add("Your third input is entered on the input text box")
aLs.add("Your fourth input is entered on the input text box")
aLs.add("Your fifth input is entered on the input text box")
aLs.add("Your sixth input is entered on the input text box")
aLs.add("Your seventh input is entered on the input text box")
aLs.add("Your eigth input is entered on the input text box")
aLs.add("Your ninth input is entered on the input text box")
aLs.add("Your tenth input is entered on the input text box")
}
}
fun main(args : Array<String>) {
println("Welcome to my domain its the third example that related to the kotlin arrayList")
var aL=ArrayList<String>()
aL.add("Its a January month")
aL.add("Its a February month")
aL.add("Its a March month")
aL.add("Its a April month")
aL.add("Its a May month")
aL.add("Its a June month")
aL.add("Its a July month")
aL.add("Its a August month")
aL.add("Its a September month")
aL.add("Its a October month")
aL.add("Its a November month")
aL.add("Its a December month")
for(i in aL)
print("The arryList are iterated please find your input values $i ")
println()
aL.set(5,"Thank you users have a nice day your input months are validated and")
for(j in aL)
print("Have a Nice day users please try again $j ")
val t=Test()
println(t.first())
}
Output:
In the above example, we used arrayList classes and its methods like add() for to perform the operations from the user end.
Example #2
Code:
package one;
interface mnths {
var vars: String
fun sample():String
fun sample1() {
var aL=ArrayList<String>()
aL.add("Your first input is Tajmahal")
aL.add("Your second input is PAris Palace")
aL.add("Your third input is Mysore Palace")
aL.add("Your fourth input is Thanjavur temple")
aL.add("Your fifth input is Madurai")
aL.add("Your sixth input is Coimbatore")
aL.add("Your seventh input is Tiruppur")
aL.add("Your Eigth input is Erode")
aL.add("Your Eigth input is Chennai")
aL.remove("Your Eigth input is Erode")
}
}
fun main(args : Array<String>) {
println("Welcome to my domain its the second example that related to the kotlin arrayList")
val first = listOf("Tajmahal", "PAris Palace", "Mysore Palace", "Thanjavur temple","Madurai","Coimbatore","Tiruppur","Erode","Chennai")
var second: List<String> = first.filter { s -> s == "Chennai" }
var third = first.singleOrNull { it == "Madurai" }
if (third != null) {
print("Your input value is found:$third")
} else {
print("Your input values are not matching also its not found!")
}
var Places = arrayListOf("Tajmahal", "PAris Palace", "Mysore Palace","Thanjavur temple","Madurai","Coimbatore","Tiruppur","Erode","Chennai")
var out = Places.find { s -> s == "Chennai" }
println(out)
}
Output:
In the second example, we used interfaces and arrayList methods like add() and remove() for to perform the tasks.
Example #3
Code:
package one;
fun main(args : Array<String>) {
println("Welcome to my domain its the third example that related to the kotlin arrayList")
val lst: ArrayList<String> = ArrayList<String>(10)
lst.add("your first input is TV")
lst.add("your second input is Fridge")
lst.add("your third input is Washing machine")
lst.add("your fourth input is Computer")
lst.add("your fifth input is UPS")
lst.add("your sixth input is LapTop")
lst.add("your seventh input is grinder")
lst.add("your eigth input is mobile")
lst.add("your ninth input is sofa")
lst.add("your tenth input is chairs")
println("Your total output is based on the inputs")
for (j in lst) {
println(j)
}
println("We can retrieved the elements by using arrayList.get(3)")
println( lst.get(2))
println("We can get the index of the element by using arrayList.indexOf(\"UPS\")")
println(lst.indexOf("UPS"))
}
Output:
In the final example, we used additional arrayList methods like get() and indexof() methods to fetch the backend results.
Conclusion
In kotlin collections is one of the util package and its more useful for to perform the operations like add, remove and fetch the datas from backend to UI. Like that, arrayList is one of the classes, and its methods for performing the data operations in the applications depend on the requirement.
Recommended Articles
This is a guide to Kotlin arrayList. Here we discuss the introduction, syntax, and working of arrayList in kotlin along with examples and code implementation. You may also have a look at the following articles to learn more –