链接列表 - 删除(对象)方法工作奇怪 - 删除最后一个元素无法正常工作



我有带测试程序的LinkedList。正如您在该程序中看到的,我将一些学生添加到列表中。我可以删除它们。如果我选择s1,s2,s3或az s4删除,一切都运行正常,我的列表打印正确,元素数量信息正确。但是如果我删除最后一个元素(在这种情况下- 5)关于元素数量的信息仍然是正确的,但是这个元素仍然被打印出来。为什么会这样呢?我错在哪里?

public class Lista implements List {
private Element head = new Element(null); //wartownik
private int size; 
public Lista(){
    clear();
}
public void clear(){
    head.setNext(null);
    size=0;
}
public void add(Object value){
    if (head.getNext()==null) head.setNext(new Element(value)); 
    else {
        Element last = head.getNext();
        //wyszukiwanie ostatniego elementu
    while(last.getNext() != null)
        last=last.getNext();
    // i ustawianie jego referencji next na nowowstawiany Element
    last.setNext(new Element(value));}
    ++size;
}

 public Object get(int index) throws IndexOutOfBoundsException{
    if(index<0 || index>size) throw new IndexOutOfBoundsException();
    Element particular = head.getNext();
    for(int i=0; i <= index; i++)
        particular = particular.getNext();
    return particular.getValue();
}  
public boolean delete(Object o){
    if(head.getNext() == null) return false;
    if(head.getNext().getValue().equals(o)){
        head.setNext(head.getNext().getNext());
        size--;
        return true;
    }
    Element delete = head.getNext();
    while(delete != null && delete.getNext() != null){
        if(delete.getNext().getValue().equals(o)){
            delete.setNext(delete.getNext().getNext());
                            size--;
            return true;
        }
        delete = delete.getNext();
    }
    return false;
}
public int size(){
    return size;
}
public boolean isEmpty(){
    return size == 0;
}
public IteratorListowy iterator() {
    return new IteratorListowy();
}
public void wyswietlListe() {
    IteratorListowy iterator = iterator();
    for (iterator.first(); !iterator.isDone(); iterator.next())
    {
        System.out.println(iterator.current());
    }
    System.out.println();
}
public void infoOStanie() {
    if (isEmpty()) {
      System.out.println("Lista pusta.");
  }
  else
  {
      System.out.println("Lista zawiera " + size() + " elementow.");
  }
}
private static final class Element{
    private Object value; 
    private Element next; //Referencja do kolejnego obiektu
    public Element(Object value){ 
        setValue(value); 
      }
    public void setValue(Object value) {
        this.value = value;
    }
    public Object getValue() {
        return value;
    }
    //ustawia referencję this.next na obiekt next podany w atgumencie
    public void setNext(Element next) {
        if (next != null)
        this.next = next;
    }
    public Element getNext(){
        return next;
    }
}
private class IteratorListowy implements Iterator{
private Element current;
 public IteratorListowy() {
   current = head;
} 
public void next() {
   current = current.next;
}   
public boolean isDone() {
   return current == null;
} 
 public Object current() {
   return current.value;
}
 public void first() {
   current = head.getNext();
}
}
}

测试
public class Program {
public static void main(String[] args) {
  Lista lista = new Lista();
  Iterator iterator = lista.iterator();
  Student s1 = new Student("Kowalski", 3523);
  Student s2 = new Student("Polański", 45612);
  Student s3 = new Student("Karzeł", 8795);
  Student s4 = new Student("Pałka", 3218);
  Student s5 = new Student("Konowałek", 8432);
  Student s6 = new Student("Kłopotek", 6743);
  Student s7 = new Student("Ciołek", 14124);
  lista.add(s1);
  lista.add(s2);
  lista.add(s3);
  lista.add(s4);
  lista.add(s5); 
  lista.wyswietlListe();
  lista.delete(s5);
  lista.wyswietlListe();

  lista.infoOStanie();
  lista.clear();
  lista.infoOStanie();
}
}

问题是您的setNext(Element next)方法不设置任何如果next == null。这就是列表最后一个元素的情况。

所以当你调用delete.setNext(delete.getNext().getNext());时,实际上没有设置任何东西,因为delete.getNext().getNext()null !

删除setNext中的if (next != null)条件,它将工作

相关内容

  • 没有找到相关文章

最新更新