Kotlin list is a generic ordered collection of elements. Kotlin has two types of lists, immutable lists (cannot be modified) and mutable lists (can be modified).
Read-only lists are created with listOf() whose elements can not be modified and mutable lists are created with mutableListOf() method where we alter or modify the elements of the list.
Kotlin program of the list contains Integers –
fun main(args: Array<String>) {
val a = listOf('1', '2', '3')
println(a.size)
println(a.indexOf('2'))
println(a[2])
}
Output:
3
1
3
Kotlin program of the list contains Strings –
fun main(args: Array<String>) {
//creating list of strings
val a = listOf("Ram", "Shyam", "Raja", "Rani")
println("The size of the list is: "+a.size)
println("The index of the element Raja is: "+a.indexOf("Raja"))
println("The element at index "+a[2])
for(i in a.indices){
println(a[i])
}
}
Output:
The size of the list is: 4
The index of the element Raja is: 2
The element at index Raja
Ram
Shyam
Raja
Rani
Hope you understand the list and types of lists in kotlin in the next blog we are going to learn Arraylist in kotlin.
Thank you
The journey of solving bug and completing project on time in Kotlin can be challenging and lonely. If you need help regarding other sides to Kotlin, we’re here for you!
Commentaires