Java - 具有自己的 hasNext() 和 next() 的可迭代:如何做对?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15863920/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 21:05:51  来源:igfitidea点击:

Java - Iterable with own hasNext() and next(): how to do it right?

javaiteratoriterable

提问by Big_Chair

I have a class that implements Iterable, but with own hasNext()and next()methods like this:

我有一个实现 Iterable 的类,但有自己的hasNext()和这样的next()方法:

public class BLMovie implements Iterable<BLMovieFrame> {

@Override
public Iterator<BLMovieFrame> iterator() {
    iteratorVar = 0;
    return liste.iterator();
}

public boolean hasNext() {
    if (loop)
        return true;
    return iteratorVar < liste.size();
}

public BLMovieFrame next() {
    iteratorVar = (iteratorVar++)%(liste.size()-1);
    return liste.get(iteratorVar);
}

public void remove() {
    throw new UnsupportedOperationException();
}

I'm pretty sure that's not the best way to do it,
the other threads about Iterable did not seem to be concerned with own next methods, so does someone have an advice for me?

我很确定这不是最好的方法,
关于 Iterable 的其他线程似乎并不关心自己的下一个方法,所以有人对我有建议吗?

回答by Perception

Your methods are defined on the wrong class. The hasNext, nextand removemethods need to be defined on the Iteratorimplementation, not on the Iterable. If you had placed an @Override annotation on each method the compiler would have informed you of this mistake.

您的方法定义在错误的类上。的hasNextnextremove方法需要在规定的Iterator执行,而不是在Iterable。如果您在每个方法上放置了 @Override 注释,编译器就会通知您这个错误。

As it is, you can either:

照原样,您可以:

  • Create an anonymous inner class for your iterator, and move those methods into its body
  • Create a standalone class for the iterator, and once again, move those methods into it
  • 为您的迭代器创建一个匿名内部类,并将这些方法移动到其主体中
  • 为迭代器创建一个独立的类,并再次将这些方法移入其中

Note that when you do this you will no longer be able to take advantage of the embedded list's iterator, but you will effectively be defining your own (though, you could wrap over it, of course).

请注意,当您这样做时,您将无法再利用嵌入列表的迭代器,但您将有效地定义自己的迭代器(当然,您可以将它包装起来)。

回答by Alexis C.

Actually you won't deal with your iterator methods since you're actually returning the iterator of your object liste.

实际上,您不会处理迭代器方法,因为您实际上是在返回 object 的迭代器liste

If you want to implement your own iterator, the best way is to create an anonymous inner class.

如果要实现自己的迭代器,最好的方法是创建一个匿名内部类。

    @Override
        public Iterator<BLMovieFrame> iterator() {
            // TODO Auto-generated method stub      
            return new Iterator <BLMovieFrame> (){

                @Override
                public boolean hasNext() {
                   if (loop)
                         return true;
                   return iteratorVar < liste.size();
              }

                @Override
                public BLMovieFrame next() {
                         iteratorVar = (iteratorVar++)%(liste.size()-1);
              return liste.get(iteratorVar);
              }
             @Override
            public void remove() {
                     throw new UnsupportedOperationException();
             }              
            };