List — Creating / listOf() / mutableListOf()
In Kotlin, use listOf() to create an immutable list and mutableListOf() to create a mutable list. You can access elements by index and use basic properties such as size and isEmpty().
Syntax
// Create an immutable list
val fruits = listOf("Apple", "Banana", "Orange")
// Create a mutable list
val numbers = mutableListOf(1, 2, 3)
numbers.add(4)
numbers.remove(2)
// Index access
val first = fruits[0] // "Apple"
val last = fruits.last() // "Orange"
// Size and empty check
println(fruits.size) // 3
println(fruits.isEmpty()) // false
println(fruits.contains("Banana")) // true
Method List
| Method / Property | Description |
|---|---|
| listOf(elements...) | Creates an immutable list (List<T>). |
| mutableListOf(elements...) | Creates a mutable list (MutableList<T>). |
| emptyList() | Creates an empty immutable list. |
| list[i] | Returns the element at index i. Throws IndexOutOfBoundsException if out of range. |
| list.getOrNull(i) | Returns the element at index i, or null if out of range. |
| list.size | Returns the number of elements. |
| list.isEmpty() | Returns whether the list is empty. |
| list.contains(element) | Returns whether the element is in the list. Can also be used with the in operator. |
| list.first() / last() | Returns the first or last element. Throws NoSuchElementException if the list is empty. |
| list.firstOrNull() / lastOrNull() | Returns the first or last element, or null if the list is empty. |
| MutableList.add(element) | Appends an element to the end of the list. |
| MutableList.remove(element) | Removes the first occurrence of the element. |
| MutableList.removeAt(i) | Removes the element at index i. |
Sample Code
fun main() {
// Immutable list
val fruits = listOf("Apple", "Banana", "Orange", "Grape")
println("fruits: $fruits")
println("Size: ${fruits.size}")
// Index access
println("First: ${fruits[0]}")
println("Last: ${fruits.last()}")
println("Second: ${fruits[1]}")
// getOrNull — safe even when out of range
println(fruits.getOrNull(10)) // null
// Search
println("Contains Banana: ${"Banana" in fruits}") // true
println("Contains Melon: ${"Melon" in fruits}") // false
println("Index of Orange: ${fruits.indexOf("Orange")}") // 2
println()
// Mutable list
val numbers = mutableListOf(10, 20, 30)
println("Initial: $numbers")
numbers.add(40) // Append to end
numbers.add(1, 15) // Insert at index
println("After add: $numbers") // [10, 15, 20, 30, 40]
numbers.remove(20) // Remove by value
numbers.removeAt(0) // Remove by index
println("After remove: $numbers") // [15, 30, 40]
numbers[0] = 100 // Update value
println("After update: $numbers") // [100, 30, 40]
println()
// Empty check
val empty: List<Int> = emptyList()
println("Empty: ${empty.isEmpty()}")
println("firstOrNull: ${empty.firstOrNull()}") // null
}
Notes
Kotlin collections make an explicit distinction between immutable (read-only) and mutable. A list created with listOf() cannot be modified; use mutableListOf() when you need to modify the list. This distinction makes your code's intent clear.
ArrayList<T>() is the same as Java's ArrayList and is treated internally as a MutableList. The idiomatic Kotlin approach is to use mutableListOf().
For filtering and transforming lists, see Lists — filter() / map(). For iteration, see Lists — forEach() / forEachIndexed().
If you find any errors or copyright issues, please contact us.