我是靠谱客的博主 孝顺万宝路,这篇文章主要介绍java array iterator_Java普通数组返回iterator,现在分享给大家,希望可以做个参考。

今天纠结了好一会儿不知道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内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部