- Android Development with Kotlin
- Marcin Moskala Igor Wojda
- 421字
- 2021-07-02 18:48:36
Arrays
In Kotlin, arrays are represented by the Array class. To create an array in Kotlin, we can use a number of Kotlin standard library functions. The simplest one is arrayOf():
val array = arrayOf(1,2,3) // inferred type Array<Int>
By default, this function will create an array of boxed Int. If we want to have an array containing Short or Long, then we have to specify the array type explicitly:
val array2: Array<Short> = arrayOf(1,2,3) val array3: Array<Long> = arrayOf(1,2,3)
As previously mentioned, using boxed representations may decrease application performance. That's why Kotlin has a few specialized classes representing arrays of primitive types to reduce boxing memory overhead: ShortArray, IntArray, LongArray, and so on. These classes have no inheritance relation to the Array class, although they have the same set of methods and properties. To create instances of this class, we have to use the corresponding factory function:
val array = shortArrayOf(1, 2, 3) val array = intArrayOf(1, 2, 3) val array = longArrayOf(1, 2, 3)
It's important to notice and keep in mind this subtle difference, because those methods look similar, but create different type representations:
val array = arrayOf(1,2,3) // 1 val array = longArrayOf(1, 2, 3) // 2
- The generic array of boxed Long elements (inferred type: Array<Long>).
- The array containing primitive Long elements (inferred type: LongArray).
Knowing the exact size of an array will often improve performance, so Kotlin has another library function, arrayOfNulls, that creates an array of a given size filled with null elements:
val array = arrayOfNulls(3) // Prints: [null, null, null] println(array) // Prints: [null, null, null]
We can also fill a predefined size array using a factory function that takes the array size as the first parameter and a lambda that can return the initial value of each array element given its index as the second parameter:
val array = Array (5) { it * 2 } println(array) // Prints: [0, 2, 4, 8, 10]
We will discuss lambdas (anonymous functions) in more detail in Chapter 5, Functions as First Class Citizen. Accessing array elements in Kotlin is done the same way as in Java:
val array = arrayOf(1,2,3) println(array[1]) //Prints: 2
Element are also indexed the same way as in Java, meaning the first element has index 0, the second has index 1, and so on. Not everything works the same and there are some differences: the main one is that arrays in Kotlin, unlike in Java, are invariant. We will discuss variance in Chapter 6, Generics Are Your Friends.
- AngularJS Web Application Development Blueprints
- SEO實戰密碼
- Python 3破冰人工智能:從入門到實戰
- 零基礎學Python網絡爬蟲案例實戰全流程詳解(高級進階篇)
- C語言程序設計
- iOS開發實戰:從入門到上架App Store(第2版) (移動開發叢書)
- C語言程序設計
- Python語言實用教程
- 區塊鏈技術進階與實戰(第2版)
- Learning Material Design
- Raspberry Pi Robotic Projects(Third Edition)
- Deep Learning with R Cookbook
- Mastering VMware Horizon 7(Second Edition)
- Mastering Android Studio 3
- Kotlin進階實戰