public void add(Object obj){
if(first==null){
Node n=new Node();
n.setPrevious(null);
n.setObj(obj);
n.setNext(null);
first=n;
last=n;
}else{
Node n=new Node();
n.setPrevious(last);
n.setObj(obj);
last.setNext(n);//是这么写,为什么这么写,下面的语句不是把last给覆盖掉了么,真不知道这语句有何用,但是少了这语句,使用get(index)读取时马上报错
last=n;
}
size++;
Node 的类是这样的
package collction;
public class Node {
Node previous;
Object obj;
Node next;
public Object getPrevious() {
return previous;
}
public void setPrevious(Node previous) {
this.previous = previous;
}
public Object getObj() {
return obj;
}
public void setObj(Object obj) {
this.obj = obj;
}
public Object getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
} |