访问循环外的所有数组值并对它们进行分组

时间:2016-05-20 15:22:13

标签: java arrays

如何访问循环外的所有值或数组?我想在循环外访问它是因为我需要根据availableID对它们进行分组。我试图将它们分组到循环中,结果不像我预期的那样..

预期结果:

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 

但事实证明是这样的:

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 

我想我应该将它们分组到循环之外,所以这是我的逻辑:

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  

此代码只能检索数组的最后一个值。但我想访问数组的所有数据..我该如何解决这个问题?或者我是否必须逐个嵌套循环每个条件?

1 个答案:

答案 0 :(得分:0)

在您的所有设置中,可能会更难以使用。我已经重写了你正在尝试的东西以及availId的所有内容。我在记事本中打了这个,所以请原谅任何问题。

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;
            }
        }
    }
}