Home > Enterprise >  3D array output null in java?
3D array output null in java?

Time:09-23

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]   " ");
                }
            }
        }
    }
}

CodePudding user response:

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]   " ");
                }
            }
        }
    }
}
  • Related