以矩阵格式java打印2d数组

以矩阵格式java打印2d数组,java,Java,如何以精细格式打印二维数组? 我想打印如下所示的矩阵,数字最多4个空格,小数最多2个空格 例如xxxx.xx double A[][]= { { 3.152 ,96.1 , 77.12}, { 608.12358 , -5.15412456453 , -36.1}, { -753..555555, 6000.156564 , -155.541654} }; //I need this output 3.15 | 96.10 | 77.12 608.1

如何以精细格式打印二维数组?

我想打印如下所示的矩阵,数字最多4个空格,小数最多2个空格 例如xxxx.xx

 double A[][]= {
    { 3.152 ,96.1 , 77.12},
    { 608.12358 , -5.15412456453 , -36.1},
    { -753..555555,  6000.156564 , -155.541654}
};

//I need this output
   3.15 |   96.10 |   77.12
 608.12 |   -5.15 |  -36.10
-753.55 | 6000.15 | -155.54
这里有一种方法:

// Convert to String[][]
int cols = A[0].length;
String[][] cells = new String[A.length][];
for (int row = 0; row < A.length; row++) {
    cells[row] = new String[cols];
    for (int col = 0; col < cols; col++)
        cells[row][col] = String.format("%.2f", A[row][col]);
}

// Compute widths
int[] widths = new int[cols];
for (int row = 0; row < A.length; row++) {
    for (int col = 0; col < cols; col++)
        widths[col] = Math.max(widths[col], cells[row][col].length());
}

// Print
for (int row = 0; row < A.length; row++) {
    for (int col = 0; col < cols; col++)
        System.out.printf("%" + widths[col] + "s%s",
                          cells[row][col],
                          col == cols - 1 ? "\n" : " | ");
}
这里有一种方法:

// Convert to String[][]
int cols = A[0].length;
String[][] cells = new String[A.length][];
for (int row = 0; row < A.length; row++) {
    cells[row] = new String[cols];
    for (int col = 0; col < cols; col++)
        cells[row][col] = String.format("%.2f", A[row][col]);
}

// Compute widths
int[] widths = new int[cols];
for (int row = 0; row < A.length; row++) {
    for (int col = 0; col < cols; col++)
        widths[col] = Math.max(widths[col], cells[row][col].length());
}

// Print
for (int row = 0; row < A.length; row++) {
    for (int col = 0; col < cols; col++)
        System.out.printf("%" + widths[col] + "s%s",
                          cells[row][col],
                          col == cols - 1 ? "\n" : " | ");
}
这里有一种方法:

// Convert to String[][]
int cols = A[0].length;
String[][] cells = new String[A.length][];
for (int row = 0; row < A.length; row++) {
    cells[row] = new String[cols];
    for (int col = 0; col < cols; col++)
        cells[row][col] = String.format("%.2f", A[row][col]);
}

// Compute widths
int[] widths = new int[cols];
for (int row = 0; row < A.length; row++) {
    for (int col = 0; col < cols; col++)
        widths[col] = Math.max(widths[col], cells[row][col].length());
}

// Print
for (int row = 0; row < A.length; row++) {
    for (int col = 0; col < cols; col++)
        System.out.printf("%" + widths[col] + "s%s",
                          cells[row][col],
                          col == cols - 1 ? "\n" : " | ");
}
这里有一种方法:

// Convert to String[][]
int cols = A[0].length;
String[][] cells = new String[A.length][];
for (int row = 0; row < A.length; row++) {
    cells[row] = new String[cols];
    for (int col = 0; col < cols; col++)
        cells[row][col] = String.format("%.2f", A[row][col]);
}

// Compute widths
int[] widths = new int[cols];
for (int row = 0; row < A.length; row++) {
    for (int col = 0; col < cols; col++)
        widths[col] = Math.max(widths[col], cells[row][col].length());
}

// Print
for (int row = 0; row < A.length; row++) {
    for (int col = 0; col < cols; col++)
        System.out.printf("%" + widths[col] + "s%s",
                          cells[row][col],
                          col == cols - 1 ? "\n" : " | ");
}
这可能会有帮助
publicstaticvoidmain(字符串[]args){
双A[][]={
{ 3.152 ,96.1 , 77.12},
{ 608.12358 , -5.15412456453 , -36.1},
{ -753.555555,  6000.156564 , -155.541654}
};
//格式中的字符数,此处XXXX.XX长度为7
整数格式长度=7;
//遍历数组
对于(inti=0;这可能会有所帮助
publicstaticvoidmain(字符串[]args){
双A[][]={
{ 3.152 ,96.1 , 77.12},
{ 608.12358 , -5.15412456453 , -36.1},
{ -753.555555,  6000.156564 , -155.541654}
};
//格式中的字符数,此处XXXX.XX长度为7
整数格式长度=7;
//遍历数组
对于(inti=0;这可能会有所帮助
publicstaticvoidmain(字符串[]args){
双A[][]={
{ 3.152 ,96.1 , 77.12},
{ 608.12358 , -5.15412456453 , -36.1},
{ -753.555555,  6000.156564 , -155.541654}
};
//格式中的字符数,此处XXXX.XX长度为7
整数格式长度=7;
//遍历数组
对于(inti=0;这可能会有所帮助
publicstaticvoidmain(字符串[]args){
双A[][]={
{ 3.152 ,96.1 , 77.12},
{ 608.12358 , -5.15412456453 , -36.1},
{ -753.555555,  6000.156564 , -155.541654}
};
//格式中的字符数,此处XXXX.XX长度为7
整数格式长度=7;
//遍历数组
对于(int i=0;我读了关于。读了关于。读了关于。读了关于。