September 2019
Intermediate to advanced
462 pages
11h 3m
English
The Array<T> class represents an array of values in Kotlin. Use arrays only when low-level optimization is desired; otherwise, use other data structures like List, which we’ll see later in this chapter.
The easiest way to create an array is using the arrayOf() top-level function. Once you create an array, you may access the elements using the index [] operator.
To create an array of Strings, for example, pass the desired values to the arrayOf() function:
| | val friends = arrayOf("Tintin", "Snowy", "Haddock", "Calculus") |
| | |
| | println(friends::class) //class kotlin.Array |
| | println(friends.javaClass) //class [Ljava.lang.String; |
| | println("${friends[0]} and ${friends[1]}") //Tintin ... |
Read now
Unlock full access