https://kotlinlang.org logo
#announcements
Title
# announcements
e

elect

09/21/2020, 9:32 AM
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

Big Chungus

09/21/2020, 9:35 AM
If you need continue & break, then for is the only choice, no matter the use-case
e

elect

09/21/2020, 9:35 AM
ok, thanks
b

Big Chungus

09/21/2020, 9:35 AM
Well you could also use while, but that's even dirtier
e

elect

09/21/2020, 9:36 AM
yep, I was looking to something more concise and still efficient..
p

PHaroZ

09/21/2020, 9:47 AM
e

elect

09/21/2020, 9:49 AM
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

Big Chungus

09/21/2020, 9:55 AM
True, but you cannot use break & continue in DSLs
p

PHaroZ

09/21/2020, 10:03 AM
sorry, I didn't seen the PS
n

Nir

09/21/2020, 12:45 PM
You can still use a regular for loop with asReversed
b

Big Chungus

09/21/2020, 12:46 PM
Adds unecessary overhead when compared with downTo
n

Nir

09/21/2020, 12:46 PM
Seems pretty premature optimization-y :-)
👍 1
It's more readable IMHO
2 Views