Updated April 4, 2023
Introduction to Kotlin List
Kotlin list is one of the features and it’s the interface for generic collection datas to store the elements it will be inherited with the collection class it’s mutable and the methods are supported only for readable functionalities the list elements are stored in the sequence order and it will insert only through the order contains the index number range which is same as the array the position in the collection is more preserved like other since the list is the mutable we cannot update the individual elements of the list or adding items to the list.
Syntax
The kotlin language has default classes, methods, and variables to implement the application with more sophisticated. Like that list is one of the collection interfaces and it is used to store and retrieve the data from backend to the front end.
fun main(args:Array<String>)
{
var lst=listOf(values) //listOf() is the default method and it is used to read the datas and it’s a fixed size
loops iteration
---some logic codes depends on the requirement---
}
The above code is the basic syntax and the listOf() method is used to store the elements in the database with the sequence order.
How does the list work in Kotlin?
The list is one of the kotlin interfaces and it is used to store and retrieve the elements in the database. It has default methods like listOf() for sorting the datatype values each element value will be stored as a separate index and the list is immutable one and it cannot be updated as the individual elements of the list or add the items to the original list. If we want to create a list that could be the mutable one and it will use as the MutableList<T> class with the ‘T’ generic class to define the mutable list. Mainly List interface has many default methods for performing the operations in the application among that some of the methods like listOf() get(), filter() are the built-in methods for achieving the kotlin operations in the application. In the kotlin list, the interface accepts abstract methods and listOf<Any>() method will utilize with any of the datatypes which are used by the kotlin language. Each method has own feature and valid statement condition for accessing the elements and also the return type of the element at the given index from the list it returns the boolean condition if the list size is empty otherwise it will return false and other default exceptions in the console.
Examples of Kotlin List
Here are the following examples mentioned below:
Example #1
Code:
interface first {
fun methd1() {
println("Thank you for declaring the first interface with the method to write the list interface logic codes")
}
}
interface second {
fun methd2() {
println("Its the second interface for declaring the method and this features for to write and call the kotlin list interface codea")
}
}
const val vars1 = "Have a Nice day users please try again"
val vars2 = methd3()
fun methd3(): String {
return "Your return value is:"
}
class Sample: first, second
fun main() {
val a = listOf('J', 'F', 'M', 'A','M','J','J','A','S','O','N','D')
println(a.size)
println(a.indexOf('2'))
println(a[2])
val x = listOf("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
println("Your String arraylist data size is: "+x.size)
println("Please find the index of the july string is : "+x.indexOf("July"))
println("Output value of the x[6]th index is"+x[6])
for(j in x.indices){
println(x[j])
}
val num = listOf("Tv first electricala appliance","Fridge is the second one","Washing machine is third one", "Fridge is fourth one", "Laptop is fifth one", "PC is sixth one", "Microwave oven is seventh one", "Ups-Inverter is eigth one", "Mobile is electronic device","Fan is tenth one")
val num1 = num.get(5)
println(num1)
val num2 = num[6]
println(num2)
val index1 = num.indexOf("Washing machine")
println("The first index of number is $index1")
val index3 = num.lastIndex
println("The last index of the list is $index3")
val ob = Sample()
ob.methd1()
ob.methd2()
println("The kotlin codes which used to declare the variable values using $vars1")
println("Value of the string type: $vars2")
var z = 400
println("The value of a is = $z");
var b = "The string inputs"
println("The value of b is = $b")
var c = 0x0C
println("The value of c is = $c")
var d = 0b10
println("The value of d is = $d")
var e = 122_222_333_444
println("The value of e is= $e");
var f = 2340_320000L
println("The value of f is = $f")
var g = 0x0C_0c_0f
println("The value of g is = $g")
var h = 0b10_1000_10
println("The value of h is = $h")
}
Output:
In the above example, we used to perform the list operations using the built-in methods.
Example #2
Code:
fun main(args: Array<String>)
{
val HomeAppliances = listOf("Bed", "Pillows", "Cort", "Locker", "Travel Bag", "Wall watch", "Cleaning equipments")
for (ha in HomeAppliances) {
print("$ha, ")
}
println()
for (i in 0 until HomeAppliances.size) {
print("${HomeAppliances[i]} ")
}
println()
HomeAppliances.forEachIndexed({i, j -> println("Home Appliances[$i] = $j")})
val it: ListIterator<String> = HomeAppliances.listIterator()
while (it.hasNext()) {
val i = it.next()
print("$i ")
}
println()
}
Output:
The second example we used to store the home appliances using the listOf() method.
Example #3
Code:
fun main(args: Array<String>)
{
val numberlist = listOf(11, 22, 33, 44, 55, 66, 77, 88, 99,110 )
val ascendingorder = numberlist.sorted()
println(ascendingorder)
val descendingorder = numberlist.sortedDescending()
println(descendingorder)
val numberlists = listOf(77, 44, 22, 11, 33, 55, 66, 88, 99,110 )
val output = numberlists.contains(2)
if (output)
println("The numberlist contains 2")
else
println("The numberlist does not contain 2")
val results = numberlists.containsAll(listOf(4, -3))
if (results)
println("The numberlist contains 5 and -2")
else
println("The numberlist does not contain 5 and -2")
}
Output:
In the final example, we used listOf() methods in the list interface which is used to perform the sorting operations in the task.
Conclusion
In kotlin language, we used a lot of util package concepts like list, map,..etc. These interfaces and classes are used to perform the data operations in the application by using the default classes and methods to start the user-defined tasks. It is supported and depreciated the older methods when we used and call the same in the main function.
Recommended Articles
This is a guide to Kotlin List. Here we discuss the introduction, syntax, and working of the list in Kotlin along with different examples and code implementation. You may also have a look at the following articles to learn more –