在arraylist中搜索具有特定值的对象的索引

时间:2017-11-13 05:24:23

标签: java arraylist

我正在编写一个程序来收集和组织班级信息,包括班级中的学生,按ID和成绩记录。我可以根据需要添加学生,并根据需要进行打印(所以我知道条目是正确的),但是当我去搜索学生时,搜索方法只能找到最后输入的学生ID。为什么我的搜索功能出现故障?

搜索功能:

public static int idFinder(ClassSection classOne) {
      int studentID = 0;
      String intString = "a Student ID to search for:";
      int studentIndex = 0;
      System.out.println("Please enter a Student ID to search for:");
        studentID = intChecker(intString);
      for (int i = 0; i < classOne.students.size(); i++) {
          int studentIdTest = classOne.students.get(i).getStudentID();
            if (studentIdTest == studentID) {
                studentIndex = i;
            } else if (studentIdTest != studentID){
                studentIndex = -1;
            }
      }
        return studentIndex;
  }

ID Finder模块:

import java.util.*;


public class ClassSection {
    //instance variables
     protected int crnNum = 0;
     protected String deptCode = "null";
     protected int courseNum = 0;
     protected String instructMode = "null";
     protected String meetingDays = "null";
     protected String meetingTimesStart = "null";
     protected String meetingTimesEnd = "null";
     protected int classCapacity = 0;
     protected int classEnrollment = 0;
     protected int instructorID = 0;
     protected ArrayList<StudentEnrollee> students = new ArrayList<StudentEnrollee>();

    //constructors
        public ClassSection(int crnNum, String deptCode, int courseNum, String instructMode, String meetingDays,
                String meetingTimesStart, String meetingTimesEnd, int classCapacity, int classEnrollment,
                int instructorID) {
            super();
            this.crnNum = crnNum;
            this.deptCode = deptCode;
            this.courseNum = courseNum;
            this.instructMode = instructMode;
            this.meetingDays = meetingDays;
            this.meetingTimesStart = meetingTimesStart;
            this.meetingTimesEnd = meetingTimesEnd;
            this.classCapacity = classCapacity;
            this.classEnrollment = classEnrollment;
            this.instructorID = instructorID;
        }


    public ClassSection() {
        super();
        this.crnNum = 0;
        this.deptCode = "";
        this.courseNum = 0;
        this.instructMode = "";
        this.meetingDays = "";
        this.meetingTimesStart = "";
        this.meetingTimesEnd = "";
        this.classCapacity = 0;
        this.classEnrollment = 0;
        this.instructorID = 0;
    }



    //getters and setters
        public void getStudents() {
            this.students.forEach(System.out::println);
        }

        public int getStudentIDByIndex(int index) {
            return this.students.get(index).getStudentID();
        }

        public void addStudent(StudentEnrollee student) {
            this.students.add(student);
        }

        public void removeStudent(int removalIndex) {
            this.students.remove(removalIndex);
        }

        public void changeAddStudentGrade(int studentIndex, int studentGrade) {
            this.students.get(studentIndex).setStudentGrade(studentGrade);
        }

        public int getCrnNum() {
            return crnNum;
        }

        public void setCrnNum(int crnNum) {
            this.crnNum = crnNum;
        }
        public String getDeptCode() {
            return deptCode;
        }
        public void setDeptCode(String deptCode) {
            this.deptCode = deptCode;
        }
        public int getCourseNum() {
            return courseNum;
        }
        public void setCourseNum(int courseNum) {
            this.courseNum = courseNum;
        }
        public String getInstructMode() {
            return instructMode;
        }
        public void setInstructMode(String instructMode) {
            this.instructMode = instructMode;
        }
        public String getMeetingDays() {
            return meetingDays;
        }
        public void setMeetingDays(String meetingDays) {
            this.meetingDays = meetingDays;
        }

        public String getMeetingTimesStart() {
            return meetingTimesStart;
        }


        public void setMeetingTimesStart(String meetingTimesStart) {
            this.meetingTimesStart = meetingTimesStart;
        }


        public String getMeetingTimesEnd() {
            return meetingTimesEnd;
        }

        public void setMeetingTimesEnd(String meetingTimesEnd) {
            this.meetingTimesEnd = meetingTimesEnd;
        }

        public int getClassCapacity() {
            return classCapacity;
        }
        public void setClassCapacity(int classCapacity) {
            this.classCapacity = classCapacity;
        }
        public int getClassEnrollment() {
            return classEnrollment;
        }
        public void setClassEnrollment(int classEnrollment) {
            this.classEnrollment = classEnrollment;
        }
        public int getInstructorID() {
            return instructorID;
        }
        public void setInstructorID(int instructorID) {
            this.instructorID = instructorID;
        }




        //mutators
        @Override
        public String toString() {
            return String.format("Crn Number: %20s \nDept Code: %20s \nInstruction Mode: %20s"
                    + " \nCourse Number: %20s \nClass Capacity: %20s \nClass Enrollment: %20s"
                    + " \nMeeting Days: %20s \nMeeting Times: %8$20s - %9$2s \nInstructor ID: %10$20s \n" + Arrays.toString(students.toArray()).replace("[", "").replace(", S","S").replace("]", "").trim(), 
                    crnNum, deptCode, instructMode, courseNum, classCapacity, classEnrollment, meetingDays,
                    meetingTimesStart, meetingTimesEnd, instructorID);
        }



}

ClassSection类:

public class StudentEnrollee {
    protected int studentID = 0;
    protected int studentGrade = 0;




    public StudentEnrollee() {
        super();
        studentID = 0;
        studentGrade = 0;
    }

    public StudentEnrollee(int studentID, int studentGrade) {
        super();
        this.studentID = studentID;
        this.studentGrade = studentGrade;
    }

    //setters & getters
    public int getStudentID() {
        return studentID;
    }
    public void setStudentID(int studentID) {
        this.studentID = studentID;
    }
    public int getStudentGrade() {
        return studentGrade;
    }
    public void setStudentGrade(int studentGrade) {
        this.studentGrade = studentGrade;
    }

    @Override
    public String toString() {
        return String.format("Student ID: %20s  \nStudent Grade: %20s \n", studentID, studentGrade);
    } 



}

学生参与者类(arraylist中的对象来自):

  public static int intChecker(String object) {
    boolean correctInput = false;
    int userInput = 0;

    while (!correctInput) {
        try {
            userInput = scanner.nextInt();
            correctInput = true;
        } catch(InputMismatchException e) {
            System.out.println("I'm sorry, that doesn't seem to be a number");
            System.out.println("Please enter " +object);
            scanner.next();
        } 
    }

    return userInput;
}


  public static boolean yesNoBool() {
      String yesNo = "";
      boolean yesNoBool = false;
      System.out.println("Please enter Y/N");
        yesNo = scanner.next();
      while ((!yesNo.equalsIgnoreCase("n") && !yesNo.equalsIgnoreCase("y"))){
            System.out.println("I'm sorry, please enter Y/N");
                yesNo = scanner.next();
        } 
      if (yesNo.equalsIgnoreCase("y")) {
          yesNoBool = true;
      } else if (yesNo.equalsIgnoreCase("n")) {
          yesNoBool = false;
      }


      return yesNoBool; 
  }

intchecker和yesNoBool方法只是错误检查和确认功能,但这里只是以防万一:

{{1}}

2 个答案:

答案 0 :(得分:0)

gcc方法中,即使找到了ID,也会继续搜索该ID。在idFinder循环中,您需要在找到ID时停止。

将for循环修改为:

for

答案 1 :(得分:0)

找到匹配后,您必须打破循环,否则studentIndex将覆盖其余循环的记录。你也不需要if(studentIdTest != studentID),其他方面的工作也一样。

在idFinder方法中添加Break in循环,如下所示:

for (int i = 0; i < classOne.students.size(); i++) {
      int studentIdTest = classOne.students.get(i).getStudentID();
        if (studentIdTest == studentID) {
            studentIndex = i;
            break;
        } else{
            studentIndex = -1;
        }
  }

建议::您可以使用-1初始化studentIndex,而不是在未找到匹配项的每次迭代中设置它。

我希望这能解决你的问题。