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

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.

主站蜘蛛池模板: 区。| 荆门市| 年辖:市辖区| 岳阳市| 屏山县| 白河县| 昆明市| 蒲江县| 禹城市| 西峡县| 焦作市| 南安市| 出国| 沾益县| 安仁县| 勃利县| 奇台县| 霍山县| 涞源县| 罗山县| 壶关县| 潞城市| 边坝县| 英山县| 巴马| 汨罗市| 隆德县| 大连市| 周宁县| 博野县| 靖安县| 谢通门县| 瓮安县| 永寿县| 英吉沙县| 玛纳斯县| 武强县| 义乌市| 平乐县| 东莞市| 璧山县|