Java 使用迭代器的NoTouchElementException问题
当我的程序运行到集合的for时,我有这个printStackTraceJava 使用迭代器的NoTouchElementException问题,java,iterator,nosuchelementexception,Java,Iterator,Nosuchelementexception,当我的程序运行到集合的for时,我有这个printStackTrace Exception in thread "main" java.util.NoSuchElementException: No next element at positionalList.NodePositionList$IteratorList.next(NodePositionList.java:177) at positionalList.NodePositionList$IteratorList.next(Node
Exception in thread "main" java.util.NoSuchElementException: No next element
at positionalList.NodePositionList$IteratorList.next(NodePositionList.java:177)
at positionalList.NodePositionList$IteratorList.next(NodePositionList.java:1)
at positionalList.Ricerca.DFS(Ricerca.java:130)
at positionalList.Ricerca.main(Ricerca.java:291)
我编写了自己的迭代器,使用head节点和tail节点(键设置为null)轻松查找列表的开头和结尾。此类位于包位置列表中的NodePositionList类内
private class IteratorList<T> implements Iterator<K> {
protected NodePositionList<K> npl;
protected Position<K> p;
@SuppressWarnings("unused")
public IteratorList(NodePositionList<K> n) {
this.npl = n;
Position<K> p = (npl.isEmpty()) ? null : npl.first();
}
@Override
public boolean hasNext() {
return p != tail;
}
@Override
public K next() throws NoSuchElementException {
if (p == null) {
throw new NoSuchElementException("No next element");
}
K toReturn = p.element();
p = (p == npl.getTail()) ? null : npl.next(p);
return toReturn;
}
@Override
public void remove() {
if (p == null) {
throw new NoSuchElementException("No element to remove");
}
p = npl.remove(p);
}
}
私有类迭代器列表实现迭代器{
受保护的节点位置列表npl;
保护位p;
@抑制警告(“未使用”)
公共迭代器列表(节点位置列表n){
这是1.npl=n;
位置p=(npl.isEmpty())?null:npl.first();
}
@凌驾
公共布尔hasNext(){
返回p!=尾部;
}
@凌驾
public K next()抛出NoTouchElementException{
if(p==null){
抛出新的NoSuchElementException(“无下一个元素”);
}
K toReturn=p.元素();
p=(p==npl.getTail())?null:npl.next(p);
回归回归;
}
@凌驾
公共空间删除(){
if(p==null){
抛出新的NoSuchElementException(“没有要删除的元素”);
}
p=不良贷款移除(p);
}
}
我用这个代码称它,它属于包“algoritmo”
公共静态无效DFS(TDAGraph g){
对于(顶点v:g.顶点()){
if(v.getColor()==VertexColor.WHITE){
探访(g,v);;
}
}
}
构造函数将是这样的
public IteratorList(NodePositionList<K> n){
this.npl = n;
this.p = (npl.isEmpty()) ? null : npl.first();
}
公共迭代器列表(节点位置列表n){
这是1.npl=n;
this.p=(npl.isEmpty())?null:npl.first();
}
构造函数将是这样的
public IteratorList(NodePositionList<K> n){
this.npl = n;
this.p = (npl.isEmpty()) ? null : npl.first();
}
公共迭代器列表(节点位置列表n){
这是1.npl=n;
this.p=(npl.isEmpty())?null:npl.first();
}
问题出在您的构造函数中:
public IteratorList(NodePositionList<K> n){
this.npl = n;
Position<K> p = (npl.isEmpty()) ? null : npl.first();
}
或:
公共迭代器列表(节点位置列表n){
这是1.npl=n;
this.p=(npl.isEmpty())?null:npl.first();
}
问题出在您的构造函数中:
public IteratorList(NodePositionList<K> n){
this.npl = n;
Position<K> p = (npl.isEmpty()) ? null : npl.first();
}
或:
公共迭代器列表(节点位置列表n){
这是1.npl=n;
this.p=(npl.isEmpty())?null:npl.first();
}
public IteratorList(NodePositionList<K> n){
this.npl = n;
this.p = (npl.isEmpty()) ? null : npl.first();
}