使用数组在java中创建表

时间:2017-02-22 17:00:43

标签: java arrays multidimensional-array

我正在尝试使用数组在java中创建一个表,这是我到目前为止所做的代码:

int[ ][ ] aryNumbers = new int[6][5];

aryNumbers[0][0] = 10;
aryNumbers[0][1] = 12;
aryNumbers[0][2] = 43;
aryNumbers[0][3] = 11;
aryNumbers[0][4] = 22;

aryNumbers[1][0] = 20;
aryNumbers[1][1] = 45;
aryNumbers[1][2] = 56;
aryNumbers[1][3] = 1;
aryNumbers[1][4] = 33;

aryNumbers[2][0] = 30;
aryNumbers[2][1] = 67;
aryNumbers[2][2] = 32;
aryNumbers[2][3] = 14;
aryNumbers[2][4] = 44;

aryNumbers[3][0] = 40;
aryNumbers[3][1] = 12;
aryNumbers[3][2] = 87;
aryNumbers[3][3] = 14;
aryNumbers[3][4] = 55;

aryNumbers[4][0] = 50;
aryNumbers[4][1] = 86;
aryNumbers[4][2] = 66;
aryNumbers[4][3] = 13;
aryNumbers[4][4] = 66;

aryNumbers[5][0] = 60;
aryNumbers[5][1] = 53;
aryNumbers[5][2] = 44;
aryNumbers[5][3] = 12;
aryNumbers[5][4] = 11;

int rows = 6;
int columns = 5;

int i, j;

for (i=0; i < rows ; i++) {
    for (j=0; j < columns ; j++) {
       System.out.print( aryNumbers[ i ][ j ] + " " );
    }

    System.out.println( "" );
}

它以行和列打印出我想要它做的事情,但我想在每个行和列中添加标题/标签,将其命名为表格。我怎么能这样做?

2 个答案:

答案 0 :(得分:0)

你通过尝试来学习。

/* TODO: Print out column heading here */

for (i=0; i < rows ; i++) {

    /* TODO: Print out i-th row label here */

    for (j=0; j < columns ; j++) {
        System.out.print( aryNumbers[ i ][ j ] + " " );
    }

    System.out.println( "" );
}

填写TODO&#39>。

答案 1 :(得分:0)

for (int x = 0; x < columns; x++){
  System.out.print("COLUMN LABEL ");
  if(x == columns - 1){
    System.out.print("\n");
  }
}
for (i=0; i < rows ; i++) {
        for (j=0; j < columns ; j++) {
           System.out.print( aryNumbers[ i ][ j ] + " " );
        }

   System.out.println( "ROW LABEL " );
}