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

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.

主站蜘蛛池模板: 绩溪县| 长宁区| 准格尔旗| 景德镇市| 休宁县| 榆树市| 达日县| 广宁县| 家居| 焦作市| 清水县| 江孜县| 凤阳县| 呈贡县| 梨树县| 曲松县| 上犹县| 聂拉木县| 宁国市| 新龙县| 朔州市| 南丰县| 高州市| 肇庆市| 察隅县| 东方市| 巴林左旗| 郓城县| 阳新县| 深水埗区| 搜索| 巴林右旗| 梅河口市| 高唐县| 锡林郭勒盟| 兴业县| 禹州市| 夏邑县| 贵港市| 洪江市| 麟游县|