In this tutorial, you shall learn how to create an array of String objects in Kotlin using arrayOf() function, with examples.

Kotlin – Create String Array

To create a String Array in Kotlin, use arrayOf() function. arrayOf() function creates an array of specified type and given elements.

Syntax

The syntax to create an Array of type String is

</>
Copy
arrayOf<String>(value1, value2, ...)

where value1, value2, and so on are the String values.

Kotlin can infer the type of Array from the argument values. Therefore, we can create an String array with initial String values as shown in the following.

</>
Copy
arrayOf(value1, value2, ...)

If no values are passed to arrayOf() function, then we must specify the datatype String as shown in the following.

</>
Copy
arrayOf<String>()

Examples

1. Create a string array with three elements

In the following program, we create a String Array with three initial elements.

Main.kt

</>
Copy
fun main(args: Array<String>) {
    val arr = arrayOf("apple", "banana", "cherry")
    for (x in arr) print("$x ")
}

Output

apple banana cherry 

Conclusion

In this Kotlin Tutorial, we learned how to create a String Array using arrayOf() function, with examples.