今天纠结了好一会儿不知道java里面一个数组类型是怎么搞出一个Iterator来返回的。。
public class TupleSet {
Tuple[] tuples;
int numSlots;
//...省略一堆
public Iteratoriterator(){
//不知道这里怎么写。。
}
}
本来已经用arraylist来替代array了,后来发现竟然可以返回iterator的匿名类Hoho~:
public Iterator iterator() {
// some code goes here
return new Iterator() {
private int nextSlot = 0;
public boolean hasNext() {
if (nextSlot >= numSlots) return false;
return true;
}
public Tuple next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return tuples[nextSlot++];
}
public void remove() {
throw new UnsupportedOperationException("[INFO] removal is not allowed");
}
};
顿时感觉好神奇。。
最后
以上就是孝顺万宝路最近收集整理的关于java array iterator_Java普通数组返回iterator的全部内容,更多相关java内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复