Java多维数组将行转换为列

时间:2016-11-29 20:30:43

标签: java arrays multidimensional-array

目前我有下面提到的表,该表有学生数据,每个学生有三个记录,这取决于学生参加考试的科目数,所以如果是更多的科目,它将成为更多的记录。除了subject_id和他/她获得该主题的标记外,字段数据也是相同的。

我需要的是,为了让每个学生单行而不是3行,subject_id将放在表格的顶部,我大致在下面的代码中显示,我名为EXPECTING_ARRAY。

我尝试使用另一个二维数组,但我遇到了如何将主题移动到列的行。

        ###################### Current Array ##########################

        +----+---------+------------+------------+-------------+----------+-------------------+-------+
        | ID | stud_id | subject_id | teacher_id | activity_id | grade_id | accademic_year_id | Mark  |
        +----+---------+------------+------------+-------------+----------+-------------------+-------+
        |  1 |       2 |          4 |          1 |           1 |        2 |                 7 | 90.00 |
        |  2 |       2 |         11 |          1 |           1 |        2 |                 7 | 43.00 |
        |  3 |       2 |         12 |          1 |           1 |        2 |                 7 | 35.00 |
        |  4 |       3 |          4 |          1 |           1 |        2 |                 7 | 40.00 |
        |  5 |       3 |         11 |          1 |           1 |        2 |                 7 | 67.00 |
        |  6 |       3 |         12 |          1 |           1 |        3 |                 7 | 88.00 |
        +----+---------+------------+------------+-------------+----------+-------------------+-------+

...

    ###################### Expecting Array #####################################
    +------+----------+------------+-------------+-------------+-------------+--------------+-----------+--------------------+
    | ID   |  stud_id |  subject_4 |  subject_11 |  subject_12 |  teacher_id |  activity_id |  grade_id |  accademic_year_id |
    +------+----------+------------+-------------+-------------+-------------+--------------+-----------+--------------------+
    | ...  |        2 |         90 |          43 |          35 |           1 |            1 |         2 |                  7 |
    | ...  |        3 |         40 |          67 |          88 |           1 |            1 |         2 |                  7 |
    +------+----------+------------+-------------+-------------+-------------+--------------+-----------+--------------------+

这是我的java代码

public static void main(String[] args){

    String[][] sheet = new String[7][8];


    //populating data [table heading];
    sheet[0][0] = "examId";
    sheet[0][1] = "studId";
    sheet[0][2] = "subject_id";
    sheet[0][3] = "teacher_id";
    sheet[0][4] = "activity_id";
    sheet[0][5] = "grade_id";
    sheet[0][6] = "accademic_year_id";
    sheet[0][7] = "Mark";

    //populating first student first row Row;
    sheet[1][0] = "1";
    sheet[1][1] = "2";
    sheet[1][2] = "4";
    sheet[1][3] = "1";
    sheet[1][4] = "1";
    sheet[1][5] = "2";
    sheet[1][6] = "7";
    sheet[1][7] = "90.00";

    //populating first student Second Row;
    sheet[2][0] = "2";
    sheet[2][1] = "2";
    sheet[2][2] = "11";
    sheet[2][3] = "1";
    sheet[2][4] = "1";
    sheet[2][5] = "2";
    sheet[2][6] = "7";
    sheet[2][7] = "43.00";

    //populating first student third Row;
    sheet[3][0] = "3";
    sheet[3][1] = "2";
    sheet[3][2] = "12";
    sheet[3][3] = "1";
    sheet[3][4] = "1";
    sheet[3][5] = "2";
    sheet[3][6] = "7";
    sheet[3][7] = "35.00";
    sheet[3][7] = "Mark";


//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //populating second student first row Row;
    sheet[4][0] = "4";
    sheet[4][1] = "3";
    sheet[4][2] = "4";
    sheet[4][3] = "1";
    sheet[4][4] = "1";
    sheet[4][5] = "2";
    sheet[4][6] = "7";
    sheet[4][7] = "90.00";

    //populating second student Second Row;
    sheet[5][0] = "5";
    sheet[5][1] = "3";
    sheet[5][2] = "11";
    sheet[5][3] = "1";
    sheet[5][4] = "1";
    sheet[5][5] = "2";
    sheet[5][6] = "7";
    sheet[5][7] = "43.00";

    //populating second student third Row;
    sheet[6][0] = "6";
    sheet[6][1] = "3";
    sheet[6][2] = "12";
    sheet[6][3] = "1";
    sheet[6][4] = "1";
    sheet[6][5] = "2";
    sheet[6][6] = "7";
    sheet[6][7] = "35.00";

//Testing Hard Coded Data   
/*for(int i = 0; i< sheet.length; i++){
        for(int j=0; j< sheet[i].length; j++){
            System.out.print(sheet[i][j] +", ");
        }
        System.out.println();
    }*/

    //I assume that the newTable will have at least one row suppose the   heading
    int new_rows = 1;

   //Finding how many rows, would be the new table
   //i skipped if rows has same stud_id, because every student will have    single row. so am count as distinct  
    for(int i = 1; i < sheet.length; i++)
        if(!sheet[i][1].equals(sheet[i-1][1]))
            new_rows++;

    int new_column = 0;
   //counting columns, i picked only first student how many rows has and then break the loop, so if it counts three same recording according to studId, it will be 3+ previous column - 2[which are marks and subject_id] 
    for(int i = 1; i<sheet.length-1; i++){
        new_column++;
        if(!sheet[i][1].equals(sheet[i+1][1]))
            break;

    }

    //checking how many rows and columns found [just for debuging]      
    System.out.println("Rows: "+new_rows+" And Columns: "+new_column);

    //creating newTable with new_records and new_column length
   // here i substract 2 [marks, and subject_id are gone]
    String[][] newTable = new String[new_rows][sheet[0].length + new_column -2];


    //here i need to populate data to newTable but how.....!!!!!!!
    for(int i = 1; i< newTable.length; i++){
        int j = 1;
        while(j < newTable[i].length){
            newTable[i][j] = sheet[i][j];
            if(!sheet[i][1].equals(sheet[i-1][1])){
                System.out.print(newTable[i][j]+" | ");
            }

            j++;
        }

        System.out.println();
    }


}

0 个答案:

没有答案