https://kotlinlang.org logo
Title
o

oday

01/14/2020, 12:09 PM
hi, how can I refactor this with
indices
val n = arr.size
    for (i in 0 until n - 1) {
        for (j in 0 until n - i - 1) {
m

Mike

01/14/2020, 12:27 PM
I believe this works, but leave it to you if it's more readable.
val arr = arrayOf(1,2,3)

arr.forEachIndexed {index, value ->
    println("v:$value")
    arr.take(arr.size - index).forEach {
        println("int:$it")        
    }
}
o

oday

01/14/2020, 12:47 PM
very nice thank you Mike
m

Mike

01/14/2020, 1:37 PM
Just be aware that
take
will create an intermediate list, so if you have a LARGE list, that could create a performance issue.