2

I've searched around and seen posts about initializing jagged 3D arrays in other languages but not in Java.

I need user input to initialize the sizes of this jagged 3D array. So, for example

UserInput: 3

Meaning I would want array[3][][]. Then

UserInput: 2, 1, 2

Meaning that array[space0][2][], array[space1][1][], array[space2][2][]

The final and third [ ] in the array is initialized to the same size as the next space's previous side. I probably said that terribly. Example,

array[space0][2][] is array[space0][2][1] and array[space1][1][] is array[space1][1][2]

I hope I've explained that well enough.

My issue is my knowledge of Java as a programming language and figuring out how to initialize stuff correctly.

I can easily take user input X, and then go array[X][][] and then take X more inputs for array[X][w,x,y][], etc, but Java does not like that.

6
  • 2
    Can you show your code attempt? Commented May 28, 2015 at 14:49
  • The main problem lies in that I'm much more comfortable using data structures in other languages that are much more flexible than regular arrays. I can't figure out the 'correct' way to initialize something like this. What I've written has not used loops because it is not initializing the way I'm envisioning when I try it line by line. Commented May 28, 2015 at 14:53
  • Here is an article for you to read. Commented May 28, 2015 at 14:54
  • Thanks, I've read the documentation. Its not possible to initialize parts of 3D arrays at a time? Commented May 28, 2015 at 15:17
  • array[space0][2][] is array[space0][2][1] - that doesn't make any sense. What was that supposed to mean? Commented May 28, 2015 at 16:14

1 Answer 1

2

If I understood your question correctly, this should be what you are looking for:

int xSize = 3;// 
int[] ySizes = {1, 2, 3};// sizes for y
int[] zSizes = {1, 2, 3, 5, 6, 7};
//             [ ] [   ] [      ]

int[][][] array3d = new int[xSize][][];// pre-initialize x 

for (int x = 0; x < array3d.length; x++)
    array3d[x] = new int[ySizes[x]][];// set the sizes of y

int pos = 0;
for (int x = 0; x < array3d.length; x++)
    for (int y = 0; y < array3d[x].length; y++)
        array3d[x][y] = new int[zSizes[pos++]];// set the sizes of z


// fill the array with some test values
for (int x = 0; x < array3d.length; x++)
    for (int y = 0; y < array3d[x].length; y++)
        for (int z = 0; z < array3d[x][y].length; z++)
            array3d[x][y][z] = x * y * z;

// print the array
System.out.println(
        Arrays.deepToString(array3d)
                .replace("],", "],\n ")
                .replace("]],", "]],\n"));
// the call to replace just improves readability

The idea is since in java 3d arrays are arrays of arrays of arrays, you create the first array, fill it with arrays of the size you want, then iterate through the new arrays and fill them with arrays of the correct size.

Sign up to request clarification or add additional context in comments.

2 Comments

I really appreciate it! I'm currently working on a way to do this in fewer lines :) Thanks!
No problem, I kinda hope this isn't the best answer, because this is a little long...

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.