[英]Why does this java iterator loop print also the first element?
import java.util.*;
public class IteratorDemo {
public static void main(String args[]) {
// Create an array list
ArrayList al = new ArrayList();
// add elements to the array list
al.add("C");
al.add("A");
al.add("E");
al.add("B");
al.add("D");
al.add("F");
// Use iterator to display contents of al
System.out.print("Original contents of al: ");
Iterator itr = al.iterator();
while(itr.hasNext()) {
Object element = itr.next();
System.out.print(element + " ");
}
System.out.println();
}
}
I don't get it. 我不明白 If
al.iterator()
returns the first element of al
, why if I put itr.next()
(which should be the second I'm guessing) in element
and then print element
does it print the first element and not the second? 如果
al.iterator()
返回al
的第一个元素,为什么如果我将itr.next()
(我猜是第二个)放在element
,然后print element
会打印第一个元素而不是第二个元素呢?
I'm new to java. 我是Java新手。 Making a parallel with c/c++, is Iterator like a pointer and Iterator.next() like the dereferenced pointer?
与c / c ++并行执行时,Iterator是否像指针一样,Iterator.next()是否像取消引用的指针一样? Can someone explain to me what's actually happening at a low level when I do what's in the code?
当我执行代码中的内容时,有人可以向我解释一下实际发生的情况吗?
Calling the iterator()
returns an iterator over the elements in the list, not the first element. 调用
iterator()
返回一个迭代器,该迭代器遍历列表中的元素,而不是第一个元素。 in fact if this returned the first element, then the condition while(itr.hasNext())
wouldnt make any sense. 实际上,如果这返回了第一个元素,则条件
while(itr.hasNext())
意义。
It's only when you call next()
that the next element in the iteration is returned. 仅当您调用
next()
,才返回迭代中的下一个元素。
So, if you want to exclude the first element then you'll need to do: 因此,如果要排除第一个元素,则需要执行以下操作:
if(itr.hasNext()) itr.next();
before the loop. 在循环之前。
Also, as an aside I'd recommend you use parameterized types rather than raw types ie your ArrayList
should be declared ArrayList<String> al = new ArrayList<>();
另外,顺便说一句,我建议你使用参数化类型,而不是原始类型,即您
ArrayList
应该声明ArrayList<String> al = new ArrayList<>();
and your iterator as Iterator<String> itr = al.iterator();
和您的迭代器为
Iterator<String> itr = al.iterator();
. 。
Given that all you want to do in this case is to print the element to the console there is no need to use an Iterator at all as it's simpler to do: 鉴于在这种情况下您要做的就是将元素打印到控制台,因此根本不需要使用Iterator,因为这样做比较简单:
al.forEach(element -> System.out.print(element + " "));
al.iterator() does not return the first element of al. al.iterator()不返回al的第一个元素。
You can imagine the iterator like in this picture. 您可以想象这张图中的迭代器。 The method iterator() provides an iterator so that you can iterate through the elements of list.
iterator()方法提供了一个迭代器,以便您可以遍历list的元素。 It doesn't give the first element when you call it.
调用它时,它不提供第一个元素。 Every time you call next() method it grabs you the next element.
每次调用next()方法时,它将抓住您的下一个元素。 That's why when you call next() for the first time it gives you the first element.
这就是为什么当您第一次调用next()时,它将为您提供第一个元素。
al.iterator()
( javadoc ) returns an Iterator to the collection (your ArrayList in this case) and not the first element. al.iterator()
( javadoc )返回一个Iterator到集合(在本例中为ArrayList),而不是第一个元素。 You then you use the iterator iterate over the elements of the collection (using hasNext
and next
) 然后,您可以使用迭代器遍历集合的元素(使用
hasNext
和next
)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.