string array literal ? How do I code it simply? string array literal ? How do I code it simply? arrays arrays

string array literal ? How do I code it simply?


You can use arrayOf(), as in

val literals = arrayOf("January", "February", "March")


arrayOf (which translates to a Java Array) is one option. This gives you a mutable, fixed-sized container of the elements supplied:

val arr = arrayOf("January", "February", "March")

that is, there's no way to extend this collection to include more elements but you can mutate its contents.

If, instead of fixed-size, you desire a variable sized collection you can go with arrayListOf or mutableListOf (mutableListOf currently returns an ArrayList but this might at some point change):

val arr = arrayListOf("January", "February", "March")    arr.add("April")

Of course, there's also a third option, an immutable fixed-sized collection, List. This doesn't support mutation of its contents and can't be extended. To create one, you can use listOf:

val arr = listOf("January", "February", "March")