Array#lastIndex property has been added to stdlib
This commit is contained in:
@@ -124,3 +124,30 @@ public inline fun <T> Array<T>.isEmpty() : Boolean = this.size == 0
|
|||||||
|
|
||||||
/** Returns the array if its not null or else returns an empty array */
|
/** Returns the array if its not null or else returns an empty array */
|
||||||
public inline fun <T> Array<T>?.orEmpty() : Array<T> = if (this != null) this else array<T>()
|
public inline fun <T> Array<T>?.orEmpty() : Array<T> = if (this != null) this else array<T>()
|
||||||
|
|
||||||
|
public inline val BooleanArray.lastIndex : Int
|
||||||
|
get() = this.size - 1
|
||||||
|
|
||||||
|
public inline val ByteArray.lastIndex : Int
|
||||||
|
get() = this.size - 1
|
||||||
|
|
||||||
|
public inline val ShortArray.lastIndex : Int
|
||||||
|
get() = this.size - 1
|
||||||
|
|
||||||
|
public inline val IntArray.lastIndex : Int
|
||||||
|
get() = this.size - 1
|
||||||
|
|
||||||
|
public inline val LongArray.lastIndex : Int
|
||||||
|
get() = this.size - 1
|
||||||
|
|
||||||
|
public inline val FloatArray.lastIndex : Int
|
||||||
|
get() = this.size - 1
|
||||||
|
|
||||||
|
public inline val DoubleArray.lastIndex : Int
|
||||||
|
get() = this.size - 1
|
||||||
|
|
||||||
|
public inline val CharArray.lastIndex : Int
|
||||||
|
get() = this.size - 1
|
||||||
|
|
||||||
|
public inline val <in T: Any?> Array<T>.lastIndex : Int
|
||||||
|
get() = this.size - 1
|
||||||
|
|||||||
@@ -39,4 +39,22 @@ class ArraysTest() : TestCase() {
|
|||||||
assertFalse(iter.hasNext, "Invalid length (hasNext)")
|
assertFalse(iter.hasNext, "Invalid length (hasNext)")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun testEmptyArrayLastIndex() {
|
||||||
|
val arr1 = IntArray(0)
|
||||||
|
assertEquals(-1, arr1.lastIndex)
|
||||||
|
|
||||||
|
val arr2 = Array<String>(0, {"$it"})
|
||||||
|
assertEquals(-1, arr2.lastIndex)
|
||||||
|
}
|
||||||
|
|
||||||
|
fun testArrayLastIndex() {
|
||||||
|
val arr1 = intArray(0, 1, 2, 3, 4)
|
||||||
|
assertEquals(4, arr1.lastIndex)
|
||||||
|
assertEquals(4, arr1[arr1.lastIndex])
|
||||||
|
|
||||||
|
val arr2 = Array<String>(5, {"$it"})
|
||||||
|
assertEquals(4, arr2.lastIndex)
|
||||||
|
assertEquals("4", arr2[arr2.lastIndex])
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user