Kotlin Lists
A list is an ordered collection of items. Items sit in a specific sequence, and each item has a numbered position called an index. Kotlin provides two kinds of lists: read-only (List) and editable (MutableList). This topic covers read-only lists.
Creating a List
val fruits = listOf("Apple", "Mango", "Banana", "Grape")
val numbers = listOf(10, 20, 30, 40, 50)
val mixed = listOf(1, "hello", true, 3.14) // mixed types (not recommended)List Structure
listOf("Apple", "Mango", "Banana", "Grape")
Index: 0 1 2 3
┌──────┬───────┬────────┬───────┐
│Apple │ Mango │Banana │ Grape │
└──────┴───────┴────────┴───────┘
Accessing Elements
val fruits = listOf("Apple", "Mango", "Banana", "Grape")
println(fruits[0]) // Apple
println(fruits[2]) // Banana
println(fruits.first()) // Apple
println(fruits.last()) // Grape
println(fruits.get(1)) // Mango (same as fruits[1])Safe Access
Accessing an index that does not exist crashes the program. Use getOrNull to avoid this:
val items = listOf("A", "B", "C")
println(items.getOrNull(1)) // B
println(items.getOrNull(10)) // null (no crash)
println(items.getOrNull(10) ?: "Not found") // Not foundList Properties and Functions
val scores = listOf(85, 92, 78, 95, 60, 88)
println(scores.size) // 6
println(scores.count()) // 6
println(scores.isEmpty()) // false
println(scores.isNotEmpty()) // true
println(scores.contains(92)) // true
println(92 in scores) // true (same as contains)
println(scores.indexOf(78)) // 2 (position of 78)Aggregation
val numbers = listOf(4, 8, 15, 16, 23, 42)
println(numbers.sum()) // 108
println(numbers.average()) // 18.0
println(numbers.min()) // 4
println(numbers.max()) // 42
println(numbers.count { it > 10 }) // 4 (items greater than 10)Transforming a List
val names = listOf("alice", "bob", "charlie")
val upper = names.map { it.uppercase() }
println(upper) // [ALICE, BOB, CHARLIE]
val long = names.filter { it.length > 3 }
println(long) // [alice, charlie]
val sorted = names.sorted()
println(sorted) // [alice, bob, charlie]
val reversed = names.reversed()
println(reversed) // [charlie, bob, alice]Iterating a List
val cities = listOf("Delhi", "Mumbai", "Pune", "Jaipur")
for (city in cities) {
println(city)
}
cities.forEach { println(it) } // same result, using lambda
cities.forEachIndexed { index, city ->
println("$index: $city")
}Sublist
val letters = listOf('A', 'B', 'C', 'D', 'E', 'F')
println(letters.subList(1, 4)) // [B, C, D] (from index 1 to 3)
println(letters.take(3)) // [A, B, C]
println(letters.drop(3)) // [D, E, F]
println(letters.takeLast(2)) // [E, F]Practical Example: Exam Scores Report
fun main() {
val scores = listOf(72, 88, 45, 95, 61, 83, 52, 79)
val passing = scores.filter { it >= 60 }
val failing = scores.filter { it < 60 }
val topScore = scores.max()
val average = scores.average()
println("All Scores : $scores")
println("Passing : $passing")
println("Failing : $failing")
println("Top Score : $topScore")
println("Average : ${"%.1f".format(average)}")
println("Pass Rate : ${passing.size * 100 / scores.size}%")
}Output:
All Scores : [72, 88, 45, 95, 61, 83, 52, 79]
Passing : [72, 88, 95, 61, 83, 79]
Failing : [45, 52]
Top Score : 95
Average : 71.9
Pass Rate : 75%