简体   繁体   中英

access all array value outside the loop and group them

How can I access all values or arrays outside the loop? I want to access it outside the loop is because I need to group them according to availableID . I have tried to group them inside the loop, the result is not as what i expected..

Expected outcome:

Student Name    Project Title   Supervisor Name Examiner Name   Start   End Date
2013438096           Hello              1025          1003        7      7  null
2013339255           Hello              1024          1001        7      7  null

Student Name    Project Title   Supervisor Name Examiner Name   Start   End Date
2013366609        Hello                 1027          1013         2      2 null 
2011679914        Hello                 1015          1014         2      2 null 
2013765397        Hello                 1026          1014         2      2 null 

However, it turns out to be like this:

Student Name    Project Title   Supervisor Name Examiner Name   Start   End Date
2013339255       Hello               1024           1001          7       7 null



Student Name    Project Title   Supervisor Name Examiner Name   Start   End Date
2013438096       Hello              1025           1003           7       7 null



Student Name    Project Title   Supervisor Name Examiner Name   Start   End Date
2013765397         Hello                1026         1014          2    2   null



Student Name    Project Title   Supervisor Name Examiner Name   Start   End Date
2011679914         Hello                1015         1014          2    2   null



Student Name    Project Title   Supervisor Name Examiner Name   Start   End Date
2013366609         Hello                1027          1013         2    2   null 

I figured i should group them outside the loop, so this is my logic:

try{ 
            String studentName = null;
            String projectTitle = null;
            String SVName = null;
            String EXName = null;
            int availID = 0;
            String day = null;
            Time start = null;
            Time end = null;
            Date date = null;

            for (int i=0 ; i<studentID.length ; i++){

               List<Object[]> list = (List<Object[]>) GenerateScheduleDAO.getFree(supervisorID[i],examinerID[i],studentID[i]);

               Object[] array = null;

               if(!list.isEmpty()){

                //declaring the variables
                  studentName = (String) array[7];
                  ....... // all variables
                  date = (Date) array[3];

                }//check list is NOT empty
                else if (list.isEmpty()){

                  list = (List<Object[]>) GenerateScheduleDAO.getOtherFree(examinerID[i], supervisorID[i],studentID[i]);


                   //declaring the variables
                   studentName = (String) array[7];
                   ....... // all variables
                   date = (Date) array[3];                

                }

   }//student loop

             if (availID == 2) {
                     out.println("<br><center><table id=\"t01\"><tr>"
                             + "<th>7Student Name</th>"
                             + "<th>Project Title</th>"
                             + "<th>Supervisor Name</th>"
                             + "<th>Examiner Name</th>"
                             + "<th>Day</th>"
                             + "<th>Start</th>"
                             + "<th>End</th>"
                             + "<th>Date</th>"
                             + "</tr>");
                     out.println("<tr>");
                     out.println("<td>"+ studentName+"</td>");
                     out.println("<td>"+ projectTitle +"</td>");
                     out.println("<td>"+ SVName +"</td>");
                     out.println("<td>"+ EXName +"</td>");
                     out.println("<td>"+ day +"</td>");
                     out.println("<td>"+ start +"</td>");
                     out.println("<td>"+ end+"</td>");
                     out.println("<td>"+ date+"</td>");
                     out.println("</tr>");
                     out.println("</center></table><br><br>");
             }//2
        else if (availID == 7) {
                    //Table consist of same attribute as above
                }//7
               if (availID == 10) {
                   //Table consist of same attribute as above
               }//10
               if (availID == 16) {
                     //Table consist of same attribute as above
               }//16
               else if (availID != 2 && availID != 7 && availID != 10 && availID == 16) {
                    //Table consist of same attribute as above
                  }//else 

 }// first try  

This codes can only retrieve the last value of the array set. But I would like to access all data of an array set.. How can i solve this? OR do i have to nested loop every condition one by one?

Over all your setup might make it harder to work with. I have rewrote what you are trying as well as grouped everything by availId. I typed this all up in notepad so please excuse any {} issues.

Comparator

Comparing Ints

public class StudentExam {

            String studentName = null;
            String projectTitle = null;
            String SVName = null;
            String EXName = null;
            int availID = 0;
            String day = null;
            Time start = null;
            Time end = null;
            Date date = null;

    public StudentExam(Object[] studentExamArray){
        this.studentName = studentExamArray[7];
        this.projectTitle = studentExamArray[]; //TODO Insert index
        this.SVName = studentExamArray[];  //TODO Insert index
        this.EXName = studentExamArray[]; //TODO Insert index
        this.availID = studentExamArray[]; //TODO Insert index
        this.day = studentExamArray[]; //TODO Insert index
        this.start = studentExamArray[]; //TODO Insert index
        this.end = studentExamArray[]; //TODO Insert index
        this.date = studentExamArray[3]; //TODO Insert index
    }
}


public static void main(String[] args){

    List<Object[]> list;

    try{ 
        List<Object[]> list = (List<Object[]>)GenerateScheduleDAO.getFree(supervisorID[i],examinerID[i],studentID[i]);
    } catch (Exception e){
        e.printstacktrace();
        // Do something ?
    }

    if(list == null || list.size() == 0){
        return; // Or throw exception
    }


    List<StudentExam> studentExams = new ArrayList(); // here you can change the list implimentation and use to make a sorted list using a comparator or we can sort it later
    for(Object[] studenExamArray : list){
        StudenExam studentExam = new StudentExam(studenExamArray);
        studentExams.add(studentExam);
    }


    // Now that you have parsed your data you can go ahead and sort it, and print it the way you want...
    Collections.sort(list, new Comparator() {
                @Override
                public int compare(StudentExam se1, StudentExam se2) {
                    return Integer.compare(se1.availID, se2.availID)
                }  
            });


    for(StudentExam studenExam : studentExams){
        switch(studentExam.availID){
            case 0:{
                // Do stuff
                break;
            }
            case 0:{
                // Do stuff
                break;
            }
            case 0:{
                // Do stuff
                break;
            }
            case 0:{
                // Do stuff
                break;
            }
            case 0:{
                // Do stuff
                break;
            }
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM