官术网_书友最值得收藏!

Arrays

In Kotlin, we can create an array by using the arrayOf() library function:

    val array = arrayOf(1, 2, 3)

Alternatively, we can create Array from an initial size and a function, which are used to generate each element:

    val perfectSquares = Array(10, { k -> k * k }) 

Unlike Java, arrays are not treated as special by the language, and are a regular collection classes. Instances of arrays provide an iterator function and a size function, as well as a get and a set function. The get and set functions are also available through bracket syntax, like many C-style languages:

    // Pull the first and second array value into variables
val element1 = array[0] val element2 = array[1]
// Set the second array item value to 5 array[2] = 5

To avoid boxing types that will ultimately be represented as primitives in the JVM, Kotlin provides alternative array classes that are specialized for each of the primitive types. This allows performance-critical code to use arrays as efficiently as they would do in plain Java. The provided classes are ByteArray, CharArray, ShortArray, IntArray, LongArray, BooleanArray, FloatArray, and DoubleArray.

主站蜘蛛池模板: 西盟| 佛冈县| 吴忠市| 怀远县| 崇义县| 武城县| 郯城县| 大石桥市| 桦川县| 克东县| 定陶县| 定襄县| 饶阳县| 罗江县| 泸溪县| 汶上县| 祥云县| 仪陇县| 探索| 勐海县| 汉阴县| 偃师市| 隆德县| 霍林郭勒市| 佛学| 亚东县| 沅陵县| 含山县| 昌都县| 招远市| 汉中市| 玉林市| 东光县| 虹口区| 霞浦县| 永胜县| 永清县| 莱西市| 仁化县| 攀枝花市| 临猗县|