Kotlin中的基本类型(二)

1.数组类型

Kotlin 中的数组经过 Array 类表达, 这个类拥有 get 和 set 函数(这些函数经过运算符重载转换为 [] 运算符), 此外还有 size 属性, 以及其余一些有用的成员函数:java

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(), 并向这个函数传递一些参数来指定数组元素的值, 这个函数的签名以下:web

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

其中,vararg表示是一个参数个数是一个变量。如:arrayOf(1, 2, 3) 建立数组, 其中的元素为 [1, 2, 3]。数组

不一样类型的元素

Kotlin还容许不一样类型元素放到一个数组中,如:svg

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

打印输出:函数

0
秦川小将
true
1.0
100.0 spa

库函数arrayOfNulls()

使用库函数 arrayOfNulls() 建立一个指定长度的数组, 其中的元素所有为 null 值。.net

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

打印输出:scala

null
null
null
null
null code

数组Array类constructor(size: Int, init: (Int) -> T)构造函数:

第一个参数是数组大小,第一个参数是一个初始化函数类型的参数。xml

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?类型。

本文分享 CSDN - 秦川小将。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。

相关文章
相关标签/搜索