Home > OS >  Java 3D array with array as elements
Java 3D array with array as elements

Time:05-28

Is quiet new to Java so not sure how to create a 3D array with for-loop array as its element:

public class Test {
    public static void main(String[] args) {
        int ASize = 20;
        int[][] loc = new int[5][5];
        for(int size = 0,xy = ASize/2; size < 5; size   ,xy  = ASize) {
            for(int size2 = 0,xy2 = ASize/2; size2 < 5; size2   ,xy2  = ASize) {
                loc[size][size2] = xy;
            }
            
        }

    }
}

Is trying to create a 3D array with element array in form of [xy,xy2] but can't figure out the correct way to express it. Supposedly Both variable xy and xy2 increment with for-loops by ASize by 20 with initial value of ASize/2. The expected output array should be:

[10,10] [30,10] [50,10] [70,10] [90,10]
[10,30] [30,30] [50,30] [70,30] [90,30]
[10,50] [30,50] [50,50] [70,50] [90,50]
[10,70] [30,70] [50,70] [70,70] [90,70]
[10,90] [30,90] [50,90] [70,90] [90,90]

So what's the correct way to write for-loops in order to create this kind of 3D array with incrementing element arrays?

CodePudding user response:

Here is one way to do it. It is a "3D" array. For an array of [R][C][Other]

  • the first level R is the number of rows.
  • the second is C the number of columns.
  • the third is the size of the array in each [r][c] cell.
int ASize = 20;
int[][][] loc = new int[5][5][2];
for (int size = 0, xy = ASize / 2; size < 5;
        size  , xy  = ASize) {
    for (int size2 = 0, xy2 = ASize / 2; size2 < 5;
            size2  , xy2  = ASize) {
        loc[size][size2][0] = xy2;
        loc[size][size2][1] = xy;
    }
    
}
for (int[][] arr : loc) {
    System.out.println(Arrays.deepToString(arr));
}

Prints

[[10, 10], [30, 10], [50, 10], [70, 10], [90, 10]]
[[10, 30], [30, 30], [50, 30], [70, 30], [90, 30]]
[[10, 50], [30, 50], [50, 50], [70, 50], [90, 50]]
[[10, 70], [30, 70], [50, 70], [70, 70], [90, 70]]
[[10, 90], [30, 90], [50, 90], [70, 90], [90, 90]]

This prints as follows.

  • Multiple dimensions in Java are really arrays of arrays.
  • so the loop iterates over each row (which is a [5][2] array) and prints each row using Arrays.deepToString()
  • Arrays.toString() is normally used to print arrays but only the first level.

CodePudding user response:

While defining a nested array you have to use a pair of square brackets [] for each level of nesting. And for "3d" array it would be [][][].

Note that there's actually no "2d" or "3d" array in Java. We can create a nested array, i.e. an array that contains other arrays.

While creating a nested array, only the length of the outer (enclosing) array needs to be provided. And you can omit declaring the sizes of the inner arrays, that can useful when inner arrays may differ in length.

I.e. this declaration will also be valid:

int[][][] loc = new int[5][][];

Another important thing about arrays that you need to be aware that although they are objects and inherit all behavior from the Object class, array don't have their own classes. As a consequence of this, if you invoke toString(), hasCode() and equals() on an array, a default implementation from the Object class would be invoked. Therefore, an attempt to print an array will result in strange symbols appearing on the console.

For that reason, Arrays utility class if your friend when need to print or compare arrays.

public static void main(String[] args) {

    int[][][] loc = new int[5][5][2];
    
    for (int row = 0; row < loc.length; row  ) {
        for (int col = 0; col < loc[row].length; col  ) {
            loc[row][col][0] = (1   2 * row) * 10;
            loc[row][col][1] = (1   2 * col) * 10;
        }
    }    

    print3DArray(loc);
}

public static void print3DArray(int[][][] arr) {
    for (int row = 0; row < arr.length; row  ) {
        for (int col = 0; col < arr[row].length; col  ) {
            System.out.print(Arrays.toString(arr[row][col])   " ");
        }
        System.out.println();
    }
}

Output:

[10, 10] [10, 30] [10, 50] [10, 70] [10, 90] 
[30, 10] [30, 30] [30, 50] [30, 70] [30, 90] 
[50, 10] [50, 30] [50, 50] [50, 70] [50, 90] 
[70, 10] [70, 30] [70, 50] [70, 70] [70, 90] 
[90, 10] [90, 30] [90, 50] [90, 70] [90, 90]
  • Related