-3
我想刪除鏈接列表中的所有元素,而不使用clear
方法。從.clear()刪除鏈接列表中的所有元素java
這是我的代碼目前
//-----inner class-----
private static class Node <T> {
private T data;
//next just points
private Node<T> next;
//constructs a node
public Node(T data, Node<T> next){
this.data = data;
this.next = next;
}
public T getData(){ return data;}
public Node<T> getNext(){ return next;}
}
private LinkedList<T> theList = new LinkedList<T>();
private Node<T> head;
private Node<T> tail = null;
private int size=0;
public ListNoOrder() {
this.head = null;
size = 0;
}
//an add method
public void add(T newElt) {
//if the new element equals null
//catch the exception
try{ if (newElt==(null));}
catch (Exception illegalArgumentException){
throw new IllegalArgumentException();}
//if it doesn't catch an exception it adds the element to the list
//and increment the size by one
//what does the head = new Node<T>(newElt, head) mean???
head = new Node<T>(newElt, head);
size++;
}
,我想要實現 如果我的當前目錄調用此方法後有四個對象的復位方法,我想該列表有0 objects
public void reset() {
head = null;
}
它應該工作,但每次我測試它說沒有東西被刪除。這只是完整代碼的一小部分。
這是'java.util.LinkedList'或您自己的實現? – Jeremy
您的問題文字與您的標題完全相反? – GhostCat
@Jeremy我自己的實現 – lionbear28