【问题标题】:access all array value outside the loop and group them访问循环外的所有数组值并将它们分组
【发布时间】:2016-05-20 15:22:13
【问题描述】:

如何访问循环外的所有值或数组?我想在循环之外访问它是因为我需要根据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  

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

【问题讨论】:

  • 当我写答案时,我意识到你的代码有一些问题。您确定您的代码可以正常运行吗?您没有设置 Object[] array 变量,而是稍后访问它。
  • @Underbalanced 当然有例外。老实说,我修剪了很多代码,所以不会太长。人们不想阅读它:(除非你要求它,否则我会发布长篇
  • 这个availID代表什么,你在哪里设置?我看到你用 0 初始化但没有设置它。
  • 如果没有代码,我的回答只会有部分帮助,因为我必须做出假设。
  • @Vijay 我在数组中设置了availID 但我没有显示它.. 但是如果你注意到的话我在评论中提到.. availID 的值是从 DAO 中检索的

标签: java arrays


【解决方案1】:

您的所有设置可能会使其更难使用。我重写了您正在尝试的内容,并按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;
            }
        }
    }
}

【讨论】:

  • 我不太明白 tbh
  • 基本上我正在创建一个类StudentExam 来帮助清除主要方法。这是您构建数据结构的地方,然后您只需对它们进行排序。你的代码丢失了很多,所以我不得不把 //TODOs.我发布的示例非常基础,不知道还有什么帮助。
  • 不使用Collections.sort还有其他方法吗?
  • @user6308605 您可以更改 ArrayList 的位置并使用排序数据结构,或者您可以使用临时数组将 Collections.sort(); 替换为手动排序。 Manually Sorting an Array in Ascending Order.
猜你喜欢
  • 2011-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-16
相关资源
最近更新 更多