0

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?

3
  • Do you really want a 2D array? Your array looks 3D with 5 rows, 5 columns, and each hold an array of 2 values? Or it could be a 2D array with a String in the form of [xy, xy2]. Commented May 27, 2022 at 16:49
  • Oh... Didn't know this is 3D array. Thought this is 2D array but with array elements. Commented May 27, 2022 at 16:53
  • It is!! But in Java, multiple dimensional arrays are just arrays of arrays of arrays, etc. So a 2D array is a single dimensional array with single dimensional array elements. And they can be different sizes so you can have jagged arrays. Commented May 27, 2022 at 17:05

2 Answers 2

2

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.
Sign up to request clarification or add additional context in comments.

Comments

1

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.