Search code examples
javaarraysmultidimensional-arraypretty-print

How to pretty print a 3D array in Java


I have a three-dimensional int array and would like to pretty-print it in this format:

[n,n,n] [n,n,n] [n,n,n]
[n,n,n] [n,n,n] [n,n,n]
[n,n,n] [n,n,n] [n,n,n]

[n,n,n] [n,n,n] [n,n,n]
[n,n,n] [n,n,n] [n,n,n]
[n,n,n] [n,n,n] [n,n,n]

[n,n,n] [n,n,n] [n,n,n]
[n,n,n] [n,n,n] [n,n,n]
[n,n,n] [n,n,n] [n,n,n]

I tried using the code below but it didn't print it right because the second and third columns have to be appended to the same line as the first column:

int[][][] hardestOne = {
    {{8, 0, 0, 0, 0, 3, 0, 7, 0}, {0, 0, 0, 6, 0, 0, 0, 9, 0}, {0, 0, 0, 0, 0, 0, 2, 0, 0}},
    {{0, 5, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 7, 0, 4, 5, 1, 0, 0}, {0, 0, 0, 7, 0, 0, 0, 3, 0}},
    {{0, 0, 1, 0, 0, 8, 0, 9, 0}, {0, 0, 0, 5, 0, 0, 0, 0, 0}, {0, 6, 8, 0, 1, 0, 4, 0, 0}}
};

public String toString() {
    String s = "";

    for (int i = 0; i < table.length; i++) {
        for (int j = 0; j < table[i].length; j++) {
            for (int k = 0; k < table[i][j].length; k++) {
                if (k == 2 || k == 5) {
                    s += table[i][j][k] + "\n";
                } else {
                    s += table[i][j][k] + "";
                }
            }
            s += "\n\n";
        }
        s += "\n";
    }

    return s;
}

UPDATE: I changed the example array to be more readable.

UPDATE 2: User @samurott gave a really good answer but there is this problem:

Consider these three lines which represent the first block in my array

{{8, 0, 0}, {0, 0, 3}, {0, 7, 0}},
{{0, 0, 0}, {6, 0, 0}, {0, 9, 0}},
{{0, 0, 0}, {0, 0, 0}, {2, 0, 0}}

Looking at those lines above the printing should look like this

[8,0,0] [0,0,0] [0,0,0]
[0,0,3] [6,0,0] [0,0,0]
[0,7,0] [0,9,0] [2,0,0]

But when I print it using his code it looks like this

[8,0,0] [0,0,3] [0,7,0]
[0,0,0] [6,0,0] [0,9,0]
[0,0,0] [0,0,0] [2,0,0]

You see I could change all the positions of the array but it's gonna get more and more confusing later on.

SOLUTION: See the solution below made by user @Alex R


Solution

  • Here would be a reusable solution for 3x3 matrices. This only works if the array contains numbers from 0 to 9. Additional formatting is needed if you want to support other integers as well.

    public final class TableFormatter {
    private static final int[][][] TABLE = {
            {{8, 0, 0, 0, 0, 3, 0, 7, 0}, {0, 0, 0, 6, 0, 0, 0, 9, 0}, {0, 0, 0, 0, 0, 0, 2, 0, 0}},
            {{0, 5, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 7, 0, 4, 5, 1, 0, 0}, {0, 0, 0, 7, 0, 0, 0, 3, 0}},
            {{0, 0, 1, 0, 0, 8, 0, 9, 0}, {0, 0, 0, 5, 0, 0, 0, 0, 0}, {0, 6, 8, 0, 1, 0, 4, 0, 0}}
    };
    
    public static void main(String[] args) {
        TableFormatter formatter = new TableFormatter(TABLE);
        System.out.println(formatter.toString());
    }
    
    private final int[][][] array3d;
    
    public TableFormatter(int[][][] array3d) {
        this.array3d = array3d;
    }
    
    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        for (int[][] array2d : array3d) {
            append2dArray(builder, array2d);
            builder.append('\n');
        }
        return builder.toString();
    }
    
    private void append2dArray(StringBuilder builder3d, int[][] array2d) {
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                StringBuilder builder2d = new StringBuilder("[");
                for (int k = 0; k < 3; k++) {
                    builder2d.append(array2d[j][(i * 3) + k]).append(',');
                }
                builder2d.deleteCharAt(builder2d.length() - 1).append("]\t");
                builder3d.append(builder2d);
            }
            builder3d.append('\n');
        }
    }
    }
    

    Output:

    [8,0,0] [0,0,0] [0,0,0] 
    [0,0,3] [6,0,0] [0,0,0] 
    [0,7,0] [0,9,0] [2,0,0] 
    
    [0,5,0] [0,0,7] [0,0,0] 
    [0,0,0] [0,4,5] [7,0,0] 
    [0,0,0] [1,0,0] [0,3,0] 
    
    [0,0,1] [0,0,0] [0,6,8] 
    [0,0,8] [5,0,0] [0,1,0] 
    [0,9,0] [0,0,0] [4,0,0] 
    

    If you want support for general matrices, you would have to pass the dimension as an argument to the constructor and update the loop variables so that they use that dimension.