ArrayList.remove() is not removing an object

后端 未结 4 558
滥情空心
滥情空心 2020-12-09 05:37

I know this is a messy implementation, but I basically have this code (I wrote all of it), and I need to be able to remove a student or instructor from the list when using t

相关标签:
4条回答
  • 2020-12-09 06:12

    Overriding the equals method of Student and Instructor will work:

    Here is an example for the Student class:

    public boolean equals(Object other){
        if(other == null) return false;
        if(other == this) return true;
        if(!(other instanceof Student)) return false;
        Student otherStudent = (Student)other;
        return otherStudent.id.equals(this.id);
    }
    

    You may also want to override hashCode():

    public String hashCode(){
        return new HashCodeBuilder(17, 31).
            append(name).
            append(id).
            toHashCode();
    }
    
    0 讨论(0)
  • 2020-12-09 06:22

    You need to Override equals and hashcode methods for collections to work properly.

    @Override
    public boolean equals(Object obj) {
        if (obj == null)
            return false;
        if (!(obj instanceof Student))
            return false;
        Student other = (Student) obj;
        return id == null ? false : id.equals(other.id);//Compare Id if null falseF
    }
    

    Since you are only using ArrayList there is hashcode method will not be used but it is still good practice to provide it.

     @Override
    public int hashCode() {
        return id == null ? 0 : id.hashCode();
    }
    
    0 讨论(0)
  • 2020-12-09 06:33

    You must correctly override the equals() method for both Student and Instructor classes.

    When overriding equals, it is good to override hashCode() as well. new Student(name, id, GPA);

    For example, something like this:

    public boolean equals(Object o) {
      if (!(o instanceof Student)) {
        return false;
      }
      Student other = (Student) o;
      return name.equals(other.name) && id.equals(other.id) && GPA == other.GPA;
    }
    
    public int hashCode() {
      return name.hashCode();
    }
    

    This way, you give a chance to the ArrayList figure out which object correspond to the one you passed as a parameter when deleting. If you don't override the above methods, it will use the default implementations in Object, which compare memory addresses which are definitely different as you remove a new Student object.

    You can read even more information about the 2 methods in the javadocs for Object.

    0 讨论(0)
  • 2020-12-09 06:38

    You didn't override the method equals for Student and Instructor.

    This method is used by the ArrayList to check wether 2 objects are the same. Without a custom implementation it will just check references, which will be different in your case since they are two different objects.

    To provide custom equality you will have to check all the fields of the involved classes to be the same. This can be done recursively by calling equals on instance variables.

    0 讨论(0)
提交回复
热议问题