is there another way to loop an ArrayList in rever...
# announcements
e
is there another way to loop an ArrayList in reverse (from last to first) without creating additional lists (such as
.reversed()
) other than
Copy code
for(i in g.windows.lastIndex downTo 0) {
    val window = g.windows[i]
? Ps: I do need
continue
and
break
inside
b
If you need continue & break, then for is the only choice, no matter the use-case
e
ok, thanks
b
Well you could also use while, but that's even dirtier
e
yep, I was looking to something more concise and still efficient..
p
e
ah, interesting.. it's a new class though
public fun <T> MutableList<T>.asReversed(): MutableList<T> = ReversedList(this)
but so is
downTo
as well actually..
although maybe it gets optimized?
b
True, but you cannot use break & continue in DSLs
p
sorry, I didn't seen the PS
n
You can still use a regular for loop with asReversed
b
Adds unecessary overhead when compared with downTo
n
Seems pretty premature optimization-y :-)
👍 1
It's more readable IMHO