【发布时间】:2014-04-06 21:24:33
【问题描述】:
我无法将元素添加到我的列表末尾。它继续添加到列表的开头。我已经有一段时间了,只是卡住了,迷路了。
public class RefUnsortedList<T> implements ListInterface<T> {
protected int numElements; // number of elements in this list
protected LLNode<T> currentPos; // current position for iteration
// set by find method
protected boolean found; // true if element found, else false
protected LLNode<T> location; // node containing element, if found
protected LLNode<T> previous; // node preceeding location
protected LLNode<T> list; // first node on the list
public RefUnsortedList() {
numElements = 0;
list = null;
currentPos = null;
}
public void add(T element) {
// Adds element to this list.
LLNode<T> newNode = new LLNode<T>(element);
newNode.setLink(list);
list = newNode;
numElements++;
这是我的主要课程:
RefUnsortedList<Patient> patient1 = new RefUnsortedList<Patient>();
Patient entry;
entry = new Patient("Tam Ngo", "0848896");
patient1.add(entry);
entry = new Patient("Mike You", "0848896");
patient1.add(entry);
System.out.println(patient1.toString());
【问题讨论】:
-
稍微思考一下你在做什么......你正在创建一个新节点,你将新节点的链接(下一个)设置为旧列表并将“第一个节点”设置为列表”到新节点。这会将节点放在列表的首位。
标签: java list generics nodes generic-list