[JAVA] Until you understand that the extended for statement and the for statement using length are the same process (handle multidimensional arrays)

Introduction

You can write a loop that retrieves the elements of an array one by one using length as a repeat condition in a for statement.

Furthermore, the extended for statement allows you to write the same process more simply, but I couldn't quite understand "why the two processes are the same".

So, this time, I would like to introduce in this article why the two processes are the same and how I understood it using multidimensional arrays.

Write with extended for statement

This time, using the following 3D array as an example, each element is output by the plintln method.


int[][][] array = { { { 1, 2 }, { 3, 4 }, { 5, 6 } }, { { 7, 8 }, { 9, 10 }, { 11, 12 } } };

First, let's write with an extended for statement.


for (int[][] tmp1 : array) {
    for (int[] tmp2 : tmp1) {
        for (int tmp3 : tmp2) {
            System.out.println(tmp3);
        }
    }
}

An integer from 1 to 12 is output to the console.

Write with a conventional for statement

Next is how to write using length as the repetition condition.

When I write using length, I used to write as follows.


for (int i = 0; i < array.length; i++) {
    for (int j = 0; j < array[i].length; j++) {
        for (int k = 0; k < array[i][j].length; k++) {
            System.out.println(array[i][j][k]);
        }
    }
}

If you look at ʻarray [i] [j] [k]`, you can see that we are extracting the elements of the array one by one.

However, when I compared this with the extended for statement above, I couldn't immediately understand that it was doing the same thing. Here's what I felt "I don't understand".

Therefore, I rewrote the writing method using length as follows.

Write in a form similar to an extended for statement about how to write using length


for (int i = 0; i < array.length; i++) {
    int[][] tmp1 = array[i];
    for (int j = 0; j < tmp1.length; j++) {
        int[] tmp2 = tmp1[j];
        for (int k = 0; k < tmp2.length; k++) {
            int tmp3 = tmp2[k];
            System.out.println(tmp3);
        }
    }
}

By writing ʻint [] [] tmp1 = array [i]; `without omitting it, I was able to make it almost the same form as the extended for statement.

Summary

If you look only at the writing method using length, it is easier to understand what the writing method using ʻarray [i] [j] [k]is doing, but it is the same as the extended for statement. I think it is easier to understand whether it will be a process if you write it without omitting ʻint [] [] tmp1 = array [i];.

If, like me, you don't really understand "why the two processes are the same", I hope this article helps you understand.

Recommended Posts

Until you understand that the extended for statement and the for statement using length are the same process (handle multidimensional arrays)
Compare the speed of the for statement and the extended for statement.
Note: Assigning and displaying values to multidimensional arrays, and practicing extended for statements