listOf

Common
JVM
JS
Native
1.0
fun <T> listOf(vararg elements: T): List<T>
(source)

Returns a new read-only list of given elements. The returned list is serializable (JVM).

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf('a', 'b', 'c')
println(list.size) // 3
println("list.contains('a') is ${list.contains('a')}") // true
println(list.indexOf('b')) // 1
println(list[2]) // c
//sampleEnd
}
Common
JVM
JS
Native
1.0
For Common

Returns a new read-only list containing only the specified object element.

The returned list is serializable (JVM).

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf('a')
println(list) // [a]
println(list.size) // 1
//sampleEnd
}
For JVM

Returns a new read-only list containing only the specified object element.

The returned list is serializable.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf('a')
println(list) // [a]
println(list.size) // 1
//sampleEnd
}
For JS, Native

Returns a new read-only list containing only the specified object element.

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf('a')
println(list) // [a]
println(list.size) // 1
//sampleEnd
}
Common
JVM
JS
Native
1.0
fun <T> listOf(): List<T>
(source)

Returns an empty read-only list. The returned list is serializable (JVM).

import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val list = listOf<String>()
println("list.isEmpty() is ${list.isEmpty()}") // true

// another way to create an empty list,
// type parameter is inferred from the expected type
val other: List<Int> = emptyList()

// Empty lists are equal

println("list == other is ${list == other}") // true
println(list) // []
// list[0] //  will fail
//sampleEnd
}