问题描述
有谁知道这段代码,问题出在哪里
import java.util.Iterator;
import java.util.NoSuchElementException;
public class AlbumIterator implements Iterator<Fotoablum>{
Fotoalbum album;
Foto aktuell;
public AlbumIterator(Fotoalbum album){
this.album=album;
this.aktuell=aktuell;
}
public boolean hasNext(){
if(this.aktuell == null){
return true;
}else{
return this.aktuell.getNächstes() != null;
}
}
public Foto next(){
if(this.aktuell == null){
this.aktuell = this.ablum.erstesFoto;
return this.aktuell;
}
if(this.aktuell.getNächstes() == null){
throw new NoSuchElementException("Keine weiteren Elemente vorhanden");
}else{
this.aktuell = this.aktuell.getNächstes();
return this.aktuell;
}
}
public void remove() {
throw new UnsupportedOperationException("Diese Aktion wird nicht unterstützt.");
}
public static void main(String[] args){
return;
}
}
错误信息
AlbumIterator.java:5: error: cannot find symbol
public class AlbumIterator implements Iterator<Fotoablum>{
^
symbol: class Fotoablum
AlbumIterator.java:22: error: cannot find symbol
this.aktuell = this.ablum.erstesFoto;
^
symbol: variable ablum
这是一篇关于java迭代器的作业。试了很多次了,还是不行,请问怎么弄,需要main函数吗?
解决方法
使用流的更简单的解决方案
您可以省去麻烦并从 Stream
创建一个:
Iterator<Foto> it =
Stream.iterate(
album.erstesFoto,f -> f.getNächstes() != null,f -> f.getNächstes())
.iterator();
您的具体编译问题
AlbumIterator.java:5: error: cannot find symbol
public class AlbumIterator implements Iterator<Fotoablum>{
^
symbol: class Fotoablum
我的德国人说你的类名有错别字,不过,你的意思是迭代 Foto
,而不是 Fotoalbum
AlbumIterator.java:22: error: cannot find symbol
this.aktuell = this.ablum.erstesFoto;
^
symbol: variable ablum
同样的错别字,这次是局部变量。