Kotlin中的基本類型(二)

1.數組類型

Kotlin 中的數組通過 Array 類表達, 這個類擁有 get 和 set 函數(這些函數通過運算符重載轉換爲 [] 運算符), 此外還有 size 屬性, 以及其他一些有用的成員函數:

public class Array<T> {
    /**
     * Creates a new array with the specified [size], where each element is calculated by calling the specified
     * [init] function. The [init] function returns an array element given its index.
     */
    public inline constructor(size: Int, init: (Int) -> T)

    /**
     * Returns the array element at the specified [index]. This method can be called using the
     * index operator:
     * ```
     * value = arr[index]
     * ```
     */
    public operator fun get(index: Int): T

    /**
     * Sets the array element at the specified [index] to the specified [value]. This method can
     * be called using the index operator:
     * ```
     * arr[index] = value
     * ```
     */
    public operator fun set(index: Int, value: T): Unit

    /**
     * Returns the number of elements in the array.
     */
    public val size: Int

    /**
     * Creates an iterator for iterating over the elements of the array.
     */
    public operator fun iterator(): Iterator<T>
}

arrayOf()

要創建一個數組, 可以使用庫函數 arrayOf(), 並向這個函數傳遞一些參數來指定數組元素的值, 這個函數的簽名如下:

public inline fun <reified @PureReifiable T> arrayOf(vararg elements: T): Array<T>

其中,vararg表示是一個參數個數是一個變量。如:arrayOf(1, 2, 3) 創建數組, 其中的元素爲 [1, 2, 3]。

不同類型的元素

Kotlin還允許不同類型元素放到一個數組中,如:

val mArray = arrayOf(0, "秦川小將", true, 1.0, 100.0f)
mArray.forEach {
    println(it)
}

打印輸出:

0
秦川小將
true
1.0
100.0

庫函數arrayOfNulls()

使用庫函數 arrayOfNulls() 創建一個指定長度的數組, 其中的元素全部爲 null 值。

val mArray = arrayOfNulls<Any>(5)
mArray.forEach {
    println(it)
}

打印輸出:

null
null
null
null
null

數組Array類constructor(size: Int, init: (Int) -> T)構造函數:

第一個參數是數組大小,第一個參數是一個初始化函數類型的參數。

val mArray = Array(5, { i -> (i * i) })
mArray.forEach {
    println(it)
}

打印輸出:

0
1
4
9
16

[] 運算符代表調用成員函數 get() 和 set()

val mArray = Array(5, { i -> (i * i) })
mArray[2] = 100
mArray.forEach {
    println(it)
}

打印輸出:

0
1
100
9
16

注意: 與 Java 不同, Kotlin 中數組的類型是不可變的. 所以 Kotlin 不允許將一個 Array 賦值給一個 Array, 否則可能會導致運行時錯誤。

原生數組類型

Kotlin 也有無裝箱開銷的專門的類來表示原生類型數組。這些原生數組類如下:

  • BooleanArray
  • ByteArray
  • CharArray
  • ShortArray
  • IntArray
  • LongArray
  • FloatArray
  • DoubleArray
  • BooleanArray

這些類和 Array 並沒有繼承關係,但它們有同樣的函數和屬性集。它們也都有相應的工廠方法:

/**
 * Returns an array containing the specified [Double] numbers.
 */
public fun doubleArrayOf(vararg elements: Double): DoubleArray

/**
 * Returns an array containing the specified [Float] numbers.
 */
public fun floatArrayOf(vararg elements: Float): FloatArray

/**
 * Returns an array containing the specified [Long] numbers.
 */
public fun longArrayOf(vararg elements: Long): LongArray

/**
 * Returns an array containing the specified [Int] numbers.
 */
public fun intArrayOf(vararg elements: Int): IntArray

/**
 * Returns an array containing the specified characters.
 */
public fun charArrayOf(vararg elements: Char): CharArray

/**
 * Returns an array containing the specified [Short] numbers.
 */
public fun shortArrayOf(vararg elements: Short): ShortArray

/**
 * Returns an array containing the specified [Byte] numbers.
 */
public fun byteArrayOf(vararg elements: Byte): ByteArray

/**
 * Returns an array containing the specified boolean values.
 */
public fun booleanArrayOf(vararg elements: Boolean): BooleanArray

原生數組定義:

val mArray: IntArray = intArrayOf(1, 3, 5, 7)

2.Any?可空類型

可空類型是Kotlin類型系統的一個特性,主要是爲了解決Java中的 NullPointerException 問題。Kotlin把可空性作爲類型系統的一部分,Kotlin編譯器可以直接在編譯過程中發現許多可能的錯誤,並減少在運行時拋出異常的可能性。

3.kotlin中的null

kotlin中null和null值是相等的,null不是Any類型,null是Any?類型。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章