Iterate Over Collections | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/7910496-programming-in-kotlin-fundamentals/lessons/23

Shouldn’t the nested loop look like this:

    for (row in 0..matrix.lastIndex) {
        for (column in 0..matrix[row].lastIndex)
            matrix[row][column] = "$row:$column"
    }

And not what is in the video:

 for (row in 0..matrix.lastIndex) {
        for (column in 0..matrix.lastIndex)
            matrix[row][column] = "$row:$column"
    }

notice the row for the index of the array (matrix).

1 Like

Hey @abdullahalhomaidhi !

Yeah that’s another way to do it! However, since matrices are usually fixed size - all rows have the same length in any number of columns, you can shortcut this by not checking it in every for loop.

The reason why I took my approach is because the matrix NxN instead of NxM. That way I could just iterate over the same number.

Thanks!

2 Likes

I am sorry but the matrix part of this course is really hard to follow. When he is explaining “break and continue” it’s beginner and then it immediately becomes advanced and i cant even follow him.

I agreed because the loop in video will make confuse and will be wrong in the other case NxM

for (row in 0..matrix.lastIndex) {
        for (column in 0..matrix.lastIndex)
            matrix[row][column] = "$row:$column"
    }