如何使用对象的属性在Java中打印一系列行?

时间:2015-11-29 05:05:33

标签: java

基本上我有一个学生对象存储在数组列表中,其属性为:

-firstName: String
-lastName: String
-studentID: String
-row: int
-column: int

现在我想在控制台中以这样的方式打印它们:

第一行

studentID1 | studentID2 | studentIDn

firstName1 | firstName2 | firstNamen

lastName1 | lastName2 | lastNamen

第二行

studentID1 | studentID2 | studentIDn

firstName1 | firstName2 | firstNamen

lastName1 | lastName2 | lastNamen

属性来自数据库并存储在学生对象中。 它们以这样的方式存储,使得它们根据列和行号排列。现在我想以这样一种方式打印上面的内容:当行属性改变时,它将打印另一个“行”的学生。

编辑:我做了什么

   public void displayStudents() {
    studentListSize = StudentList.size();
    int listIndex = 0;                          // This variable gives you the size of the list
    int columnIndex = 0;                        // The column index gives you the column you are supposedly traversing
    int resetIndex = 0;                         // This index does what its name does which is essentially to reset a value
    int continueIndex = 0;                      // Index which tells one where one supposedly ended, hence the name 'continue'

    int currentRow = seats.get(columnIndex).getStudentRow();    // The currentRow method says which row one is currently traversing

    for(listIndex = 0; listIndex < seatSize; listIndex++) {
        // if the row attribute taken from the list of seats is not equal to the current row then, that means that the row has changed and 
        // the following loops execute 
        if(seats.get(listIndex).getStudentRow() != currentRow) {

            // The following methods just display stuff. It's essentially a looped version of the commented out method below
            System.out.println();
            for(columnIndex = resetIndex; columnIndex <= continueIndex; columnIndex++) {
                System.out.printf("|%10s|",  seats.get(columnIndex).getFirstName());
            }
            System.out.println();
            for(columnIndex = resetIndex; columnIndex <= continueIndex; columnIndex++) {
                System.out.printf("|%10s|",  seats.get(columnIndex).getLastName());
            }
            System.out.println();

        // setting the reset index to the column index is done because if you don't do this the for loops below will repeat the displayed things above with a new set of columns
        resetIndex = columnIndex;
        // this sets the currentRow
        currentRow = seats.get(listIndex).getStudentRow();
        // Print the IDs
        System.out.printf("|%10s|",  seats.get(listIndex).getStudentID());
        // the assignment below ensures that the all rows are printed (for the first set)
        continueIndex = listIndex;
    }

    // The next lines are workarounds which repeat what the above does. There could have been a better way had there been a functioning GOTO keyword in Java
    System.out.println();
    for(columnIndex = resetIndex; columnIndex <= continueIndex; columnIndex++) {
        System.out.printf("|%10s|",  seats.get(columnIndex).getFirstName());
    }
    System.out.println();
    for(columnIndex = resetIndex; columnIndex <= continueIndex; columnIndex++) {
        System.out.printf("|%10s|",  seats.get(columnIndex).getLastName());
    }
    System.out.println();

我在循环中重复了代码,因为它只会打印到第二行的学生ID。

数据库来自mySQL,我刚收到结果集,基本上存储了LinkedList中的Student对象

0 个答案:

没有答案