This commit is contained in:
James Strachan
2012-06-15 12:02:17 +01:00
parent d9788f9d3c
commit c6012f1fac
2 changed files with 17 additions and 0 deletions
+8
View File
@@ -172,6 +172,14 @@ val <T> List<T>.last : T?
return if (s > 0) this.get(s - 1) else null return if (s > 0) this.get(s - 1) else null
} }
/**
* Returns the index of the last item in the list or -1 if the list is empty
*
* @includeFunctionBody ../../test/ListTest.kt lastIndex
*/
val <T> List<T>.lastIndex : Int
get() = this.size - 1
/** /**
* Returns the first item in the list * Returns the first item in the list
* *
+9
View File
@@ -1,5 +1,6 @@
package test.collections package test.collections
import java.util.ArrayList
import kotlin.test.* import kotlin.test.*
import org.junit.Test as test import org.junit.Test as test
@@ -36,4 +37,12 @@ class ListTest {
} }
assertEquals(data.size(), index) assertEquals(data.size(), index)
} }
test fun lastIndex() {
val emptyData = ArrayList<String>()
val data = arrayList("foo", "bar")
assertEquals(-1, emptyData.lastIndex)
assertEquals(1, data.lastIndex)
}
} }