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