java while (LinkedList.iterator().hasNext()) 不起作用

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

java while (LinkedList.iterator().hasNext()) does not work

javaiteratorwhile-loop

提问by Yevgraf Andreyevich Zhivago

I have the following while loop, if I put this.boatTripsList.iterator().hasNext() in the while loop condition, it throws error. When I create iterator then put in the while loop condition, it will work then. Why is this? Thanks & Regards. (the second version throws error)

我有以下 while 循环,如果我将 this.boatTripsList.iterator().hasNext() 放在 while 循环条件中,则会引发错误。当我创建迭代器然后放入 while 循环条件时,它就会工作。为什么是这样?感谢和问候。(第二个版本抛出错误)

 public Journey(List<BoatTrip> trips) {
   this.boatTripsList = new LinkedList<BoatTrip>();
   Iterator<BoatTrip> iterator = trips.iterator();
   //add the given boat trips to the boattrips list
    while (iterator.hasNext()) {
         BoatTrip thistrip = iterator.next();
         this.boatTripsList.add(thistrip);
    }
}



public Journey(List<BoatTrip> trips) {
   this.boatTripsList = new LinkedList<BoatTrip>();
   //add the given boat trips to the boattrips list
    while (trips.iterator().hasNext()) {
         BoatTrip thistrip = iterator.next();
         this.boatTripsList.add(thistrip);
    }
}

回答by fge

This is normal: if your while condition is while(trips.iterator().hasNext()), you create a new iterator each time. If your list is not empty, the condition will therefore always be true...

这是正常的:如果您的 while 条件为while(trips.iterator().hasNext()),则每次都创建一个新的迭代器。如果您的列表不为空,则条件将始终为真...

While in the loop itself, you use the iterator you created before entering the loop... As a result, you'll get a NoSuchElementExceptionwhen this iterator is empty.

在循环本身中,您使用您在进入循环之前创建的迭代器......因此,NoSuchElementException当此迭代器为空时,您将获得 a 。

Use:

利用:

final Iterator<Whatever> = list.iterator();
Whatever whatever;

while (iterator.hasNext()) {
     whatever = iterator.next();
     // do whatever stuff
}

But for walking lists, a foreach loop is preferred:

但是对于步行列表,首选 foreach 循环:

for (final BoatTrip trip: tripList)
    // do whatever is needed

And if you want to add the contents of a list to another, use .addAll():

如果要将列表的内容添加到另一个列表,请使用.addAll()

// no need for the "this" qualifier, there is no name conflict
boatTripList.addAll(trips);

回答by Quetzalcoatl

You aren't using the iteratoryou requested on the first line of your code there - you're requesting a new one each time, so it will always have a next.

你没有使用iterator你在那里的代码第一行请求的 - 你每次都请求一个新的,所以它总会有下一个。

回答by Regenschein

A call to .iterator() obtains a new iterator. If you do that in the loop, you will always obtain a new iterator rather than iterating over an existing iterator.

调用 .iterator() 获得一个新的迭代器。如果您在循环中这样做,您将始终获得一个新的迭代器,而不是迭代现有的迭代器。

回答by jsfviky

this.boatTripsList.iterator().hasNext() is wrong

this.boatTripsList.iterator().hasNext() 是错误的

this.boatTripsList.hasNext() is correct

this.boatTripsList.hasNext() 是正确的