- 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.
- Java程序設(shè)計(jì)實(shí)戰(zhàn)教程
- 零基礎(chǔ)學(xué)C++程序設(shè)計(jì)
- LabVIEW入門與實(shí)戰(zhàn)開發(fā)100例
- Mastering SVG
- 程序員面試筆試寶典
- Wireshark Network Security
- Java游戲服務(wù)器架構(gòu)實(shí)戰(zhàn)
- Flash CS6中文版應(yīng)用教程(第三版)
- 編譯系統(tǒng)透視:圖解編譯原理
- Visual Basic程序設(shè)計(jì)與應(yīng)用實(shí)踐教程
- PLC編程與調(diào)試技術(shù)(松下系列)
- 蘋果的產(chǎn)品設(shè)計(jì)之道:創(chuàng)建優(yōu)秀產(chǎn)品、服務(wù)和用戶體驗(yàn)的七個(gè)原則
- 西門子S7-200 SMART PLC編程從入門到實(shí)踐
- R Data Science Essentials
- 從Power BI到Analysis Services:企業(yè)級(jí)數(shù)據(jù)分析實(shí)戰(zhàn)