Java 2d array as table, print out indexes

老子叫甜甜 提交于 2021-01-27 20:55:53

问题


So, basically I need to print out a 2d array as a table and put indexes "around" it.

Random rnd = new Random();

    int[][] array = new int[5][5];
    for (int row = 0; row < array.length; row++) {
        for (int col = 0; col < array[row].length; col++) {

            array[row][col] = rnd.nextInt(6);
        }
    }
    for (int row = 0; row < array.length; row++) {

        for (int col = 0; col < array[row].length; col++) {

            if (col < 1) {
                System.out.print(row+" ");
                System.out.print(" " + array[row][col] + " ");
            } else {

                System.out.print(" " + array[row][col] + " ");
            }

        }
        System.out.println();
    }

}

I get this:

0  2  4  0  2  4

1  1  2  0  2  2 

2  0  1  5  4  0 

3  4  2  1  4  1 

4  2  4  3  1  3 

So the first (left) column are indexes and I need to put another column of indexes (0,1,2,3,4) on top of the "table" with "counting" starting from the second column...something like this:

  0 1 2 3 4
0 2 4 0 2 4
1 1 2 0 2 2
2 0 1 5 4 0
3 4 2 1 4 1
4 2 4 3 1 3

Sorry for any mistakes, its my first time asking here.


回答1:


Your final printed tabs has a size bigger by one than your array. So first, loop for the length of array + 1. To do this change < condition by <=.

Then you can simply identify the different cases you are going through :

  1. Top right corner is identify by row==0 and col==0. In this case just print a space
  2. First row is identify by row==0 and col!=0. In this case just print the column number in your array : col-1
  3. First column is identify by col==0 and row!=0. In this case just print the row number in your array : row-1
  4. All other values, just print them from your array : array[col-1][row-1].

You should obtain this :

for (int row = 0; row <= ROWS; row++) {
    for (int col = 0; col <= COLUMNS; col++) {
        if (col == 0 && row == 0) {  //Top right corner
            System.out.print(" ");
        } else if (row == 0) { //First row
            System.out.print(" " + (col-1));
        } else if (col == 0) { //First column
            System.out.print(" " + (row-1));
        } else { //All other cases
            System.out.print(" " + array[row-1][col-1]);
        }
    }
    System.out.println();
}



回答2:


Make a for loop before the nested for loop:

Random rnd = new Random();

final int COLUMNS = 5;
final int ROWS = 5;

int[][] array = new int[COLUMNS][ROWS];
for (int row = 0; row < array.length; row++) {
    for (int col = 0; col < array[row].length; col++) {

        array[row][col] = rnd.nextInt(6);
    }
}
System.out.print(" ");
for (int row = 0; row < array.length; row++) {
  System.out.print("  " + row );
}
System.out.println();
for (int row = 0; row < array.length; row++) {
    for (int col = 0; col < array[row].length; col++) {
        if (col < 1) {
            System.out.print(row);
            System.out.print("  " + array[row][col]);
        } else {

            System.out.print("  " + array[row][col]);
        }
    }
    System.out.println();
}

output:

   0  1  2  3  4
0  2  3  1  0  2
1  0  1  1  5  2
2  1  0  3  3  5
3  1  4  0  5  2
4  1  0  3  3  3



回答3:


You are only missing another for before the nested fors in which you print the table, where to print out first an empty character, after which the indexes needed. Otherwise, the code seems correct.

System.out.print(" ");
for(int i=0;i<cols;i++){
    System.out.print(" "+i+" ");
}


来源:https://stackoverflow.com/questions/49428270/java-2d-array-as-table-print-out-indexes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!