0

Can anyone let me know what's wrong with my remove method? I cant delete the element from the sorted arraylist

This is the code

 public static void main(String[] args) {
        SortedListInterface<Student> studList = new SortedArrayList<Student>();
        
        studList.add(new Student("Alex", "20WWW09000","ABC"));
        studList.add(new Student("Cait", "20WDA09080","DEF"));
        studList.add(new Student("Jane", "20WMC09065","GHI"));
        System.out.println(studList);
        studList.remove(new Student("Alex", "20WWW09000","ABC"));
        System.out.println(studList);

This is the output

Student ID = 20WDA09080 Student Name = Cait course = DEF

Student ID = 20WMC09065 Student Name = Jane course = GHI

Student ID = 20WWW09000 Student Name = Alex course = ABC

Student ID = 20WDA09080 Student Name = Cait course = DEF

Student ID = 20WMC09065 Student Name = Jane course = GHI

Student ID = 20WWW09000 Student Name = Alex course = ABC

Student Alex still exist in the arraylist

This is the remove method

public boolean remove(T anEntry) {
       if(isEmpty()){
           return false;
       }
       else{
           int b =0;
          while(b<length && array[b].compareTo(anEntry)<0){
            b++;
       } 
          
         if (array[b].equals(anEntry)) {
            int remove = (b+1) - 1;  //remove the gap
            int last = length -1 ;
            
             for (int c = remove; c < last; c++) {
                 array[c] = array[c+1];
             }

             length --;
             return true;
        }
       }
       return false;

    }

This is the Student class(Entity class)

public class Student extends Person implements Comparable<Student> {
    
    private String course;
     public Student(String course, String id, String name, String password) {
        super(id, name, password);
        this.course = course;
    }

    public Student(String name, String id, String course) {
        super(id, name);           //I am using this constructor to add the student
        this.course = course;
    }

   

    public Student(String id, String name) {
        super(id, name);
    }


    
    public Student(String id) {
        super(id);
    }

    public String getCourse() {
        return course;
    }

    public void setCourse(String course) {
        this.course = course;
    }

    
      public int compareTo(Student s){
          
        return thisID().compareTo(s.getId()); 
   }

    @Override
    public String toString() {
        return super.toString() + " course = " + course ;
    }    
}
Wjunnn
  • 1
  • 1
  • 1
    Have you debuugged/checked if your remove method returns true or false? would be hiugely helpfull to know which path your code takes. – OH GOD SPIDERS Mar 23 '21 at 13:12
  • Nope, it return nothing.I also curious why it does not return true or false. and this is an sorted array list – Wjunnn Mar 23 '21 at 13:18
  • 1
    How did you check what it returns? It's either throwing an exception or returning and since it does the second print statement, we can only assume it returns something. What does the "equals" method look like for your student class? – matt Mar 23 '21 at 13:20
  • But i really did not see what the remove method return. I already added the Student class there please have a look. – Wjunnn Mar 23 '21 at 13:29
  • 1
    You can assign a value, `boolean test = studList.remove(...)` also, where is your Student.equals method? If you don't override equals then `array[b].equals(anEntry)` will always be false since you are calling `new`. – matt Mar 23 '21 at 13:31
  • 1
    Maybe this helps? https://stackoverflow.com/questions/2265503/why-do-i-need-to-override-the-equals-and-hashcode-methods-in-java – matt Mar 23 '21 at 13:32
  • Ya i just tested it return false, i am thinking that will there be another way to use this remove method instead of calling ```new```? Since i didnt have the Student.equals method – Wjunnn Mar 23 '21 at 13:44
  • When you add your student, `Student alex = new Student("Alex", "20WWW09000","ABC");` Then you can `studList.add(alex)` and later remove `studList.remove(alex)`. Otherwise, write a Student.equals method. – matt Mar 23 '21 at 13:53
  • The solution given i was tried before but still cant, because i dont know how to write a Student.equals method do you have any guide on it? – Wjunnn Mar 23 '21 at 14:01
  • You have two problems here. You have a sorted list that you're developing, and you have a Student class you're developing. Maybe create a new question asking how to make an equals method for your student class. You can test your sorted list with java Integer, since they have equals and hashcode correct. – matt Mar 23 '21 at 14:03

0 Answers0