Create an Array of Strings in Kotlin
To create an array of strings in Kotlin, you can use arrayOf()
function. Call arrayOf()
function and pass the string elements as arguments to the function. The function returns an Array
created from the given string elements.
Examples
Create an Array of student names
In the following program, we create an array with the names of students in a class.
Kotlin Program
fun main(args: Array<String>) {
val names = arrayOf("Ram", "Manu", "Ajay", "Sam")
}
Create an Array of fruit names
In the following program, we create an array with the names of fruits available in an online store.
Kotlin Program
fun main(args: Array<String>) {
val fruits = arrayOf("apple", "banana", "cherry")
}