janvladimirmostert
02/18/2017, 6:29 PMmutableListOf
if you need a mutable list, listOf
simply returns type List
which is immutable. Naming convention is probably due to the Java naming convention - keep java.util.List, but make it immutable.panov.andy
02/18/2017, 11:02 PMpanov.andy
02/18/2017, 11:02 PMcoletz
02/18/2017, 11:07 PMpanov.andy
02/18/2017, 11:10 PMpanov.andy
02/18/2017, 11:18 PMpanov.andy
02/18/2017, 11:35 PMpanov.andy
02/18/2017, 11:42 PMevkaky
02/19/2017, 2:07 PMfor (i in 4 downTo 1)
but I want to iterate over List<T> like this one:
val names = listOf("bob", "ben", “nick”)
for (i in names downTo) {
// i == "nick" on the first iteration
}
It seems I can't do it with range-loop, only with old "while" loop. Am I right?bob
02/19/2017, 2:10 PMnames.reversed()
to reverse the list and iterate over it.voddan
02/19/2017, 2:13 PMnames.asReversed()
to avoid copyingmiha-x64
02/19/2017, 2:14 PMfun <T> List<T>.forEachReversed(consumer: (T)->Unit) {
// hope `this is RandomAccess`
for (i in size - 1 downTo 0) {
consumer(get(i))
}
}
evkaky
02/19/2017, 2:24 PMTristan Caron
02/19/2017, 3:44 PMTristan Caron
02/19/2017, 3:44 PMCapture d’écran 2017-02-19 à 16.44.19.png▾
user
02/19/2017, 3:47 PMhttps://kotlinlang.slack.com/files/U1DKTH3NW/F47DX3JDR/capture_d___e__cran_2017-02-19_a___16.44.19.png▾
nekoinemo
02/20/2017, 8:21 AMorangy
izorin
02/20/2017, 11:37 AMNoSuchElementException
?bernhard
02/20/2017, 11:38 AMval result = it.lastOrNull { somePredicate } ?: yourDefaultValue
izorin
02/20/2017, 11:39 AMbernhard
02/20/2017, 11:39 AMjanvladimirmostert
02/20/2017, 11:47 AMvoddan
02/20/2017, 12:44 PMbts.rybicki
02/20/2017, 1:33 PMbts.rybicki
02/20/2017, 1:33 PMbts.rybicki
02/20/2017, 1:34 PMnekoinemo
02/20/2017, 1:34 PM