-2

So I just learned doing arrays in 1D and 2D in java, but I don't seem to do 3D correct since the output seems to show null like this:

One null null
null Two null
null null Three

One null null
null Two null
null null Three

One null null
null Two null
null null Three


I want it like this:

One Two Three
One Two Three
One Two Three

One Two Three
One Two Three
One Two Three

One Two Three
One Two Three
One Two Three


SOLVED (changed the format of the array to make it simplier for me as a beginner.)

    public class Main {
    public static void main(String[] args)
    {
        String[][][] numbers = {
                                {{"One", "Two", "Three"}, {"Four", "Five", "Six"},
                                {"Seven", "Eight", "Nine"}, {"Ten", "Eleven", "Twelve"}}
                                };

        for(int i = 0; i < numbers.length; i++)
        {
            System.out.println();
            for(int j = 0; j < numbers[i].length; j++)
            {
                System.out.println();
                for(int k = 0; k < numbers[i][j].length; k++)
                {
                    System.out.print(numbers[i][j][k] + " ");
                }
            }
        }
    }
}
Haap
  • 3
  • 3
  • 1
    You assign 9 values, but you should assign 27 to have them all filled. All those coordinates you didn't assign a value to, remain null – Stultuske Sep 22 '22 at 08:14
  • And perhaps you should assign the 27 values to `numbers`, not to the non-existing `cars`... – Mihe Sep 22 '22 at 08:34
  • @Mihe Yeah sorry, my bad, I forgot to change it. – Haap Sep 22 '22 at 09:08

1 Answers1

0

You've got to fill your arrays correctly. A 3D-array in Java is just an array of 2D-arrays and a 2D-array is just an array of (1D-)arrays.

You can fill a given (1D-)array row of length 3 by assigning values to its elements, that is

row[0] = "One";
row[1] = "Two";
row[2] = "Three";

Given a 2D-Array matrix you can do this for every single row:

for (String[] row : matrix) {
    row[0] = "One";
    row[1] = "Two";
    row[2] = "Three";
}

Now, you have an array numbers of those matrices:

for (String[][] matrix : numbers) {
    for (String[] row : matrix) {
        row[0] = "One";
        row[1] = "Two";
        row[2] = "Three";
    }  
}

I've used for-each-loops here. They can be turned into normal for-loops easily:

for (int i = 0; i < numbers.length; i++) {
    String[][] matrix = numbers[i];
    for (int j = 0; j < matrix.length; j++) {
        String[] row = matrix[j];
        row[0] = "One";
        row[1] = "Two";
        row[2] = "Three";
    }
}

From that you can substitute row by matrix[j] which gives you

for (int i = 0; i < numbers.length; i++) {
    String[][] matrix = numbers[i];
    for (int j = 0; j < matrix.length; j++) {
        matrix[j][0] = "One";
        matrix[j][1] = "Two";
        matrix[j][2] = "Three";
    }
}

Of course, you can substitute matrix, too. This time by numbers[i]:

for (int i = 0; i < numbers.length; i++) {
    for (int j = 0; j < numbers[i].length; j++) {
        numbers[i][j][0] = "One";
        numbers[i][j][1] = "Two";
        numbers[i][j][2] = "Three";
    }
}

Update

You can directly integrate this in your code:

public class Main {
    public static void main(String[] args)
    {
        String[][][] numbers = new String[3][3][3];
        for (int i = 0; i < numbers.length; i++) {
            for (int j = 0; j < numbers[i].length; j++) {
                numbers[i][j][0] = "One";
                numbers[i][j][1] = "Two";
                numbers[i][j][2] = "Three";
            }
        }        

        for(int i = 0; i < numbers.length; i++)
        {
            System.out.println();
            for(int j = 0; j < numbers[i].length; j++)
            {
                System.out.println();
                for(int k = 0; k < numbers[i][j].length; k++)
                {
                    System.out.print(numbers[i][j][k] + " ");
                }
            }
        }
    }
}
Mihe
  • 2,270
  • 2
  • 4
  • 14
  • Since I'm new I've a hard time understanding what "for (String[] row : matrix)" really means so therefore I changed it to make it more simplier for me as you can see in the post I just updated. – Haap Sep 22 '22 at 10:20
  • for-each is really just that: for each element in `matrix` repeat ... See https://stackoverflow.com/a/33232565/19657183 for a very good answer. – Mihe Sep 22 '22 at 10:51
  • Alright, I just learned for each but sadly I do not understand 3D array any good, 1D and 2D I understand but 3D is more complex for me. But thanks for putting effort into it! – Haap Sep 22 '22 at 20:32
  • Take your time. Try to find good resources, Stackoverflow is neither intended to be nor is it the best place to learn Java :-) – Mihe Sep 22 '22 at 20:48
  • I understand, would it be possible to see how you would do it (whole code) so I can analyze it bit by bit for better understanding? I can only make it work with curly brackets on 3D array since I only need to initialize it like > "String[][][] names = {{{"name", "name" etc...}}} < I don't need to do it index by index myself because for each do it automatically for me. But it's totally okey if you don't wanna do it nor have time for it. Thanks anyways :) – Haap Sep 23 '22 at 09:31