java 如何检查 Iterator.next() 是否为 == null?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37878053/
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-11-03 02:54:15  来源:igfitidea点击:

How to check if Iterator.next() is == null?

javanulliteratoranonymous-class

提问by Lazlow

How do I check if the next element in the list is null ?

如何检查列表中的下一个元素是否为 null ?

while(it.hasNext()){

            System.out.println(it.next()+"\n");
        }

this is how I tried, but when the last element is null it prints it as null.

这就是我尝试的方式,但是当最后一个元素为空时,它会将其打印为空。

I tried to change it

我试图改变它

 while(it.hasNext()){
    if(it.next()==null){
    }else             
        System.out.println(it.next()+"\n");
            }

but this just makes it worst because some of the elements don't even print!

但这只会让情况变得更糟,因为有些元素甚至无法打印!

This is my Iteration method/anonymous class

这是我的迭代方法/匿名类

public Iterator<Filmi> iterator3DFilms ()throws FilmiException{
        if(filmList.isEmpty())
            throw new FilmiException("No Films on the list");
        return new Iterator<Filmi>(){
            private int index=0;
            public boolean hasNext(){
                return index <filmList.size();
            }
            public Filmi next(){
                Filmi lb = filmList.get(index++);

                if(lb.is3D()== true)
                    return lb;
                if(hasNext())
                    return next();
                return null;
            }
            public void remove(){}
        };
    }

The null print only happens at the last element Thank you.

空打印只发生在最后一个元素谢谢。

回答by Bathsheba

Naturally, code like

自然地,代码像

if (it.next() == null){
} else {
    System.out.println(it.next()+"\n");
}

will consume every other non-nullelement, as you are observing. Plus calling it.next()without checking it.hasNext()is a recipe for disaster.

null正如您所观察到的那样,将消耗所有其他非元素。加上it.next()不检查it.hasNext()就打电话是灾难的秘诀。

Why not write

为什么不写

Foo/*ToDo - use the correct type here*/ foo = it.next()
if (foo != null){
    /*ToDo*/
}

instead?

反而?

回答by Nicolas Filotto

No it cannot work this way because if it.next()is not nullyou call it.next()twice which will make you skip a value that could not even be available.

不,它不能以这种方式工作,因为如果it.next()不是,null您将调用it.next()两次,这将使您跳过一个甚至不可用的值。

Use a variable instead as next:

使用变量代替如下:

Object o = it.next();
if (o != null) {
   ...
}

回答by sab

you should use stream instead of iterator.

您应该使用流而不是迭代器。

filmList.stream().filter(film->film!=null).filter(film->film.is3D())

Edit: or, if you'r not in Java 8 :

编辑:或者,如果您不在 Java 8 中:

Predicate<Film> isNotNullAnd3D = new Predicate<Person>() {
        public boolean apply(Film f) {
            return f != null && f.is3D();
        }
    };

Collection2.filter(filmList, isNotNullAnd3D)

回答by Ivan Matavulj

You never mentioned why you use iterators explicitly in the first place. Why not use implicit iterator notation like this ? :

您一开始从未提到为什么要明确使用迭代器。为什么不使用这样的隐式迭代器符号?:

for (Film film : filmList) {
  if (film != null ){
     ....
  }
}