[英]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:我有一个 3D int 数组,想以这种格式打印它:
[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:更新 2:用户@samurott 给出了一个非常好的答案,但存在这个问题:
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解决方案:请参阅用户@Alex R提出的以下解决方案
Here would be a reusable solution for 3x3 matrices.这将是 3x3 矩阵的可重用解决方案。 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.这仅适用于数组包含从 0 到 9 的数字。如果您还想支持其他整数,则需要额外的格式。
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: 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.如果您想支持通用矩阵,则必须将维度作为参数传递给构造函数并更新循环变量,以便它们使用该维度。
I believe what you're trying to print is a 4-dimensional array, not a 3-dimensional one, as you have 4 layers of grouping.我相信您要打印的是 4 维数组,而不是 3 维数组,因为您有 4 层分组。 However, here's the modified pretty-print loop:但是,这是修改后的漂亮打印循环:
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++){
s += "[";
for (int l = 0; l < table[i][j][k].length; l++){
s += table[i][j][k][l];
if (l != table[i][j][k].length - 1){
s += ",";
}
}
s += "]";
if (k != table[i][j].length - 1){
s += " ";
}
}
s += "\n";
}
s += "\n";
}
This is the modified value table I'm using:这是我正在使用的修改后的值表:
static int[][][][] table = {
{
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
},
{
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
},
{
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}},
{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
},
};
I made very minor changes to your code.我对你的代码做了非常小的改动。 The problem is that k loops over your inner data and you put a newline in several places.问题是 k 循环遍历您的内部数据,并且您在几个地方放置了换行符。 I don't think that's what you wanted.我不认为那是你想要的。 You then add more newlines after each loop of k.然后在每个 k 循环之后添加更多换行符。 I removed the extra newlines and then fiddled slightly with making it pretty.我删除了额外的换行符,然后稍微摆弄了一下让它变得漂亮。
public class X {
int[][][] table = {
{{1, 2, 3, 4, 5, 6, 7, 8, 9}, {11, 12, 13, 14, 15, 16, 17, 18, 19}, {21, 22, 23, 24, 25, 26, 27, 28, 29}},
{{101, 102, 103, 104, 105, 106, 107, 108, 109}, {111, 112, 113, 114, 115, 116, 117, 118, 119}, {121, 122, 123, 124, 125, 126, 127, 128, 129}},
{{201, 202, 203, 204, 205, 206, 207, 208, 209}, {211, 212, 213, 214, 215, 216, 217, 218, 219}, {221, 222, 223, 224, 225, 226, 227, 228, 229}},
};
public String toString() {
String s = "";
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
s += (j == 0) ? "[" : " [";
for (int k = 0; k < table[i][j].length; k++) {
if (k > 0) {
s += ", ";
}
s += table[i][j][k];
}
s += "]";
}
s += "\n";
}
return s;
}
public static void main(String[] args) {
X foo = new X();
System.out.println(foo.toString() );
}
}
$ javac X.java && java X
[1, 2, 3, 4, 5, 6, 7, 8, 9] [11, 12, 13, 14, 15, 16, 17, 18, 19] [21, 22, 23, 24, 25, 26, 27, 28, 29]
[101, 102, 103, 104, 105, 106, 107, 108, 109] [111, 112, 113, 114, 115, 116, 117, 118, 119] [121, 122, 123, 124, 125, 126, 127, 128, 129]
[201, 202, 203, 204, 205, 206, 207, 208, 209] [211, 212, 213, 214, 215, 216, 217, 218, 219] [221, 222, 223, 224, 225, 226, 227, 228, 229]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.