如何解决迭代器hasNext用于获取最后一条记录
private static void printList(LinkedList<String> linkedList)
{
Iterator<String> i = linkedList.iterator();
while(i.hasNext())
{
System.out.println(i.next());
}
System.out.println("-------------------");
}
据我了解,i.hasNext()
方法检查下一个条目是否存在,并返回true或false。
这里上面的i.next()
方法将显示当前的链表记录,然后递增/移动到下一条记录。
如果我们在最后一条记录中,i.hasNext()
为假,那么它如何执行i.next()
的最后一次迭代?
解决方法
假设您的列表为1,2,3,4
您的迭代器hasNext()
在启动时将返回true
,因为它当前指向开始,并且下一个包含1
。使用i.next()
,您将获得1
。
现在,在倒数第二次迭代中,当光标位于3处并且您收到3
和i.next()
时。现在i.hasNext()
将返回true
,因为它有4
,您将在循环内,执行i.next()
消耗了整个列表,因此下一次调用i.hasNext()
时返回false
,终止循环。
我强烈建议您检查一下Link在OpenJDK中如何实现next
和hasNext
方法。
链表迭代器具有一个nextIndex
变量,该变量指向如果调用next
应当返回的项目的索引。如您所知,调用next
将使该变量递增。
假设您拥有列表[0,1,4]
,并且您将第三次致电next()
。之后,迭代器的状态如下:
nextIndex is pointing to this
|
V
0 1 2 3 4
^
|
return value of the third next()
我认为您有这种困惑,因为您不了解hasNext
在做什么。 hasNext
不会检查在之后 nextIndex
的索引是否为有效索引。它检查nextIndex
是否为有效索引。 hasNext
的实现方式如下:
return nextIndex < size;
只要hasNext
指向列表中的项目,那是真的。
现在让我们看看循环的下一次迭代会发生什么:
nextIndex is pointing to this
|
V
0 1 2 3 4
^
|
return value of the fourth next()
nextIndex
为4
(不要与它指向的元素混淆),它小于size
(5),因此hasNext
仍然为真,因此循环可以再运行一次:
nextIndex is pointing to this
|
V
0 1 2 3 4
^
|
return value of the fifth next()
现在nextIndex
大于size
,指向链表之外的内容,因此hasNext
为false,循环停止。 next
已返回所有5个元素。
例如,列表为1、2、3、4
请参阅java.util.LinkedList.ListItr.java
public boolean hasNext() {
return nextIndex < size;
}
nextIndex = 0,大小= 4,因此hasNext = true,那么,如果您调用next(),则它将从位置0返回的项= 1并递增nextIndex + 1
2。 nextIndex = 1,大小= 4,因此hasNext = true,然后如果您调用next(),则它将从位置1返回的项= 2,并递增nextIndex + 1
3。 nextIndex = 2,大小= 4,因此hasNext = true,然后,如果您调用next(),它将从位置2返回的项目= 3,并递增nextIndex + 1
4。 nextIndex = 3,大小= 4,因此hasNext = true,然后,如果您调用next(),则它将从位置3返回项目= 4,并递增nextIndex + 1
5。 nextIndex = 4,大小= 4,因此hasNext = FALSE
,迭代器是一个接口。这是hasNext()和next()方法如何工作的代码。
// Java code to illustrate the use of iterator
import java.io.*;
import java.util.*;
class Test {
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
list.add("D");
list.add("E");
// Iterator to traverse the list
Iterator iterator = list.iterator();
System.out.println("List elements : ");
while (iterator.hasNext())
{
System.out.print(iterator.hasNext()+" "+iterator.next() + " "+iterator.hasNext()+"\n");
}
System.out.println();
}
}
上面代码的输出:
列表元素:
true一个true
正确B正确
真C真
正确D正确
真E假
hasNext()和next()方法的定义:
public class NextFromIterator implements Iterator<String> {
private int position = -1;
private List<String> list = new ArrayList<String>() {{
add("alpha"); add("bravo"); add("charlie"); add("delta"); add("echo"); add("foxtrot");
}};
public boolean hasNext() {
return next() != null; // BAD: Call to 'next'
}
public String next() {
position++;
return position < list.size() ? list.get(position) : null;
}
public void remove() {
// ...
}
public static void main(String[] args) {
NextFromIterator x = new NextFromIterator();
while(x.hasNext()) {
System.out.println(x.next());
}
}
每个迭代next()将其指针加1,初始值是-1。在使用迭代器的实际大小检查该指针是否小于该值之后,此方法返回该元素,否则返回null。在hasNext()方法中,此next()方法针对每个循环调用,并在此检查是否等于null。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。