我的名單看起來是這樣的:有麻煩創建我自己的列表迭代器
public class SList<A> implements Iterable<A>
{
private Listelem head;
private Listelem current;
private boolean listEmpty;
private class Listelem
{
private A value;
private Listelem next;
private Listelem(A val)
{
this.value = val;
this.next = null;
}
private Listelem()
{
this.next = null;
}
public void setValue(A val)
{
this.value = val;
}
public A getValue()
{
return this.value;
}
public void setSuccessor(Listelem next)
{
this.next = next;
}
public Listelem getSuccessor()
{
return this.next;
}
}
}
我要爲這個列表創建一個迭代器,但我有一些麻煩。 在SLIST我這樣做:
@Override
public Iterator<A> iterator() {
Iterator<A> it = new Iterator<A>() {
this.current = this.head;
@Override
public boolean hasNext() {
boolean hasNext = true;
if(this.current.getSucessor == null)
{
hasNext = false;
}
return hasNext;
}
@Override
public A next() {
A next = this.current.getValue;
this.current = this.current.getSuccessor();
return next;
}
@Override
public void remove() {
// TODO Auto-generated method stub
}
};
return it;
}
我不能老是參考this.current或this.head。我想知道爲什麼這不起作用,因爲我在同一班。
太多的代碼,你至少可以指向你堅持的位?它不讓你參考它? – FaddishWorm