Lesson 11 of 17
Lists
Lists in Kotlin
Create an immutable list with listOf:
val nums = listOf(1, 2, 3, 4, 5)
Access elements by index (0-based), check size, and test membership:
println(nums[0]) // 1
println(nums.size) // 5
println(nums.contains(3)) // true
For a mutable list that you can add/remove elements from:
val items = mutableListOf("a", "b")
items.add("c")
println(items.size) // 3
Your Turn
Create a list of the first 5 odd numbers [1, 3, 5, 7, 9]. Print the list's size, its first element, its last element, and whether it contains 7.
JS Transpiler loading...
Loading...
Click "Run" to execute your code.