简体   繁体   English

ArrayList和Java方法

[英]ArrayList and methods for java

I have been working on this for hours so I hope someone can help me. 我已经为此工作了几个小时,所以我希望有人可以帮助我。 I have to create an arraylist of students and do the following commands. 我必须创建一个学生数组列表并执行以下命令。 add, find and delete students. 添加,查找和删除学生。 Anyways my add, find functions work ok, but when I try to delete students it brings up the wrong student! 无论如何,我的添加,查找功能都可以正常工作,但是当我尝试删除学生时,它会带来错误的学生! I dont' know what to do feels like I have tried everything. 我不知道该怎么办,就像我已经尝试了一切。

public void addStudent(String studentName, long studentID, String address) {
    Student newStudent = new Student ( studentName,  studentID, address);
    collegeList.add(newStudent);
}

public static  void deleteStudent() {
    Scanner input=new Scanner(System.in);
    System.out.print("Enter student ID");
    long studentNumber=input.nextLong();

    if(directory.isValidID(studentNumber) && directory. withinRange(studentNumber)) {
        System.out.print("Deleting Student");
        System.out.print(directory.findStudent(studentNumber));
        System.out.print("please confirm with y/n");
        Scanner myans=new Scanner(System.in);
        String confirmation=myans.next();

        if (confirmation.equals("y")) {
            directory.deleteStudent(studentNumber);
            System.out.print("student deleted");
        }

        if(confirmation.equals("n")) {
            System.exit(0);
        }
    }
}

/**
Searches for student based upon their student number
@param studentID unique student number for each student
@return students entire information
*/
public String findStudent(long studentID) { 
    String str;
    Student newStu;

    for (int i=0; i<collegeList.size(); i++ ) {
        newStu=collegeList.get(i);

        if(newStu.getStudentID()==studentID);

            return newStu.toString(); 
    }

    return null;
}

/**
Removing student from collegeList
@param studentID unique student number
@return none
*/
public void deleteStudent (long studentID) { 
    Student newStu;
    for (int i=0; i<collegeList.size(); i++ ) {
        newStu=collegeList.get(i);

        if (newStu.getStudentID()==studentID)
            collegeList.remove(i);
    }
}

Please correct me if I am mistaken, but it looks like your comparison is wrong. 如果我弄错了,请指正我,但看来你的比较是错误的。 It updates highest if you have found a lower score then highest (exact opposite of what you want). 如果您发现得分较低然后得分最高(与您想要的相反),它将更新最高。

highest>newStu.getQuizScore()

should perhaps be 应该是

highest<newStu.getQuizScore()

You also need to iterate the entire list to find the highest score. 您还需要遍历整个列表以找到最高分数。 Now you return the first score that is lower then the first score, but that may not be correct. 现在,您返回的第一个分数低于第一个分数,但这可能不正确。 I would do something like this: 我会做这样的事情:

public Student findHighest () {

    Student highest;
    highest=collegeList.get(0);


    for (int i=1; i<collegeList.size(); i++ ) {

        Student newStu=collegeList.get(i);

        if (highest.getQuizScore()<newStu.getQuizScore()){
            highest=newStu;
        }

    }
    return highest;

}

Sorry for any mistakes or problems with my answer, I am new to Stack Overflow. 很抱歉我的答案有任何错误或问题,我是Stack Overflow的新手。

Cheers. 干杯。

Does your Student class override the hashCode() and equals() methods? 您的Student类是否覆盖hashCode()equals()方法? If not, it won't behave properly with Java collections. 如果没有,它将不能与Java集合一起正常工作。

Then you should be able to do things like: 然后,您应该可以执行以下操作:

studentList.add(student);
...
int index = studentList.indexOf(student);
if (index != -1) return studentList.get(index);
...
studentList.remove(student);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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