Kotlin – List of Strings

To define a list of Strings in Kotlin, call listOf() function and pass all the Strings as arguments to it.

The syntax of listOf() function is

</>
Copy
fun <T> listOf(vararg elements: T): List<T>

listOf() function returns a read-only List.

Since, we are passing strings for elements parameter, listOf() function returns a List<String> object.

Example

In the following program, we will define a list of Strings using listOf() function.

Main.kt

</>
Copy
fun main(args: Array<String>) {
    val names = listOf("Abc", "Xyz", "Pqr")
    print(names)
}

Output

[Abc, Xyz, Pqr]

Conclusion

In this Kotlin Tutorial, we learned how to define a list of Strings in Kotlin, using listOf() function.