- Learn Kotlin Programming(Second Edition)
- Stephen Samuel Stefan Bocutiu
- 192字
- 2021-06-24 14:13:26
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.
- HTML5+CSS3+JavaScript從入門到精通:上冊(微課精編版·第2版)
- 程序員面試筆試寶典(第3版)
- Java系統(tǒng)分析與架構(gòu)設計
- 大學計算機基礎實驗教程
- Mastering Scientific Computing with R
- Android程序設計基礎
- UI設計全書(全彩)
- Instant Debian:Build a Web Server
- Functional Python Programming
- Web開發(fā)的平民英雄:PHP+MySQL
- Flink入門與實戰(zhàn)
- JavaEE架構(gòu)與程序設計
- Clojure Data Structures and Algorithms Cookbook
- Python趣味創(chuàng)意編程
- Mastering Unity 2017 Game Development with C#(Second Edition)