简体   繁体   English

Java中的1混乱使迭代器关闭

[英]Iterator off by 1 confusion in Java

When using iterator in Java, the last iterator is pointing one past the last node right? 在Java中使用迭代器时,最后一个迭代器指向最后一个节点之后的一个,对吗? And the value is null right? 而且该值为null对吗? Then when using 然后当使用

iter = myList.listIterator();
sum = 0;
while (iter.hasNext()) {

sum += iter.next();

}

Shouldn't there be a off-by-one problem since the last value iterator is pointing at is null? 由于迭代器指向的最后一个值是null,是否应该不存在一个单一的问题? I know in fact is there's no such off-by-one problem, but don't know why, please explain this for me. 我知道实际上不存在这样的单一问题,但不知道为什么,请为我解释一下。 Thanks in advance! 提前致谢!

Edit: sorry, too excited, didn't type complete code. 编辑:对不起,太激动了,没有键入完整的代码。

Summary: Hope this helps for future people, I was thinking about iterators as pointers in C++, but actually even if imagined as pointers, instead of pointing to nodes, they 'point' to the space between nodes (space before a node to be more precisce) as bmorris591 described. 摘要:希望这对未来的人们有所帮助,我曾考虑将迭代器作为C ++中的指针,但实际上,即使将其想象为指针,它们也“指向”节点之间的空间(而不是指向节点的空间,而指向节点之前的空间更多)。 premorce),如bmorris591所述。

next() moves the Iterator along, hasNext() only checks if there is a next. next()沿着Iterator前进, hasNext()仅检查是否存在下一个。 The Iterator doesn't "point", if anything it points at the spaces between elements. Iterator器不会“指向”,如果有的话它指向元素之间的空间。

You may want to read up on the Iterator Pattern . 您可能需要阅读Iterator Pattern

Assuming your list contains Integer instances, you can do something like the following: 假设您的列表包含Integer实例,则可以执行以下操作:

Iterator <Integer> iter = myList.listIterator();
int sum = 0;
while (iter.hasNext()) {
    sum += iter.next().intValue();
}

your code will add the result of each iter.next() to sum on each iteration of the loop. 您的代码将添加每个iter.next()的结果,以在循环的每次迭代中sum

When iter.hasNext() is called, it is not "at the first value", but just before the first value - the first call will then return that first value and update the internal state of the iterator to be just before the second value (just after the first value). 调用iter.hasNext() ,它不是“处于第一个值”,而是在第一个值之前-第一个调用将返回该第一个值,并将迭代器的内部状态更新为恰好在第二个值之前(紧随第一个值之后)。

When you get to the end, the internal state is pointing to just after the last value, so iter.hasNext() returns false and your loop exits. 当您到达末尾时,内部状态指向最后一个值之后,因此iter.hasNext()返回false并退出循环。

Now, if your iterator contains any null values, they are still returned by the calls to next() , and you'll end up with a NullPointerException (which you can avoid with a simple null check). 现在,如果您的迭代器包含任何null值,则它们仍将由next()的调用返回,并且您最终将遇到NullPointerException(可以通过简单的null检查来避免此错误)。

There is no null element returned from an Iterator when hasNext returns false . hasNext返回false时,没有从Iterator返回的null元素。

Conceptually, when hasNext returns false , the iterator is pointing "past" the last element already, but there is nothing "past" the last element, not even null . 从概念上讲,当hasNext返回false ,迭代器已经指向最后一个元素“过去”,但是没有最后一个元素“过去”,甚至没有null

If you call next when hasNext returns false , then it won't return null -- it will throw a NoSuchElementException . 如果你打电话给nexthasNext返回false ,那么它不会返回null -它会抛出一个NoSuchElementException

An iterator iterates over the list. 迭代器遍历列表。 The method hasNext() returns true when there are still elements left to iterate over and false when all elements have been processed. 当仍有剩余元素要迭代时,hasNext()方法返回true,而在处理完所有元素后返回false。 An example: 一个例子:

A list with contents [0,2,4]: 内容为[0,2,4]的列表:

iter = myList.listIterator();
iter.hasNext(); // true
iter.next(); // returns 0
iter.hasNext(); // true
iter.next(); // returns 2
iter.hasNext(); // true
iter.next(); // returns 4
iter.hasNext(); // false

Thus there is no problem with off-by-one or something like that. 因此,一站式服务或类似的服务没有问题。 Whenever hasNext() returns true, there are still elements left to iterate over; 每当hasNext()返回true时,仍然需要迭代一些元素。 next() returns that element. next()返回该元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM