how to declare string array in kotlin
Declaring a String Array in Kotlin
To declare a string array in Kotlin, follow these steps:
- Using the arrayOf() function:
- Use the
arrayOf()
function to create an array of strings. Example:
kotlin val stringArray = arrayOf("apple", "banana", "orange")
Using the Array constructor:
- Use the Array constructor to create an array of strings.
Example:
kotlin val stringArray = Array(3) { "" } stringArray[0] = "apple" stringArray[1] = "banana" stringArray[2] = "orange"
Using the emptyArray() function:
- Use the
emptyArray()
function to create an empty array of strings. Example:
kotlin val stringArray = emptyArray<String>()
Using the constructor with an initializer:
- Use the constructor with an initializer to create an array of strings.
- Example:
kotlin val stringArray = Array(3) { index -> "Element $index" }