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 ;
}
}