简体   繁体   English

为什么这个Java迭代器循环还会打印第一个元素?

[英]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 ) 然后,您可以使用迭代器遍历集合的元素(使用hasNextnext

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

相关问题 为什么java.util.Iterator不会在嵌套循环中重置? - Why java.util.Iterator does not reset in a nested loop? 对于迭代器的每个循环,跳过第一个元素 - For each loop of Iterator skips first element Java是否在执行For循环,但同时对Linked List(“ For,While”)循环使用迭代器? - Java doing a For loop but also using an iterator for Linked List ( “For, While”) Loop? 为什么使用列表迭代器会永远循环? - Why does using list iterator loop forever? 为什么迭代器未在for循环中“下移” - Why does the Iterator not “Move Next” in a for loop 为什么我的for循环仅检查第一个元素? - Why does my for loop check only the first element? 为什么要使用“迭代器”来循环遍历 ArrayList。 没有 Iterator 也可以做到,对吗? - why to use 'iterator' to loop thru an ArrayList. It can be done without Iterator also right? 循环重新启动时,为什么我的第一个打印语句打印两次? - Why does my first print statement print twice when the loop restarts? 如果在没有转换为迭代器的情况下无法获取下一个元素,为什么Java有链表? - Why does Java have a linked list if I cannot get the next element without converting to an iterator? 为什么我的 java 猜谜游戏在第一个循环后不起作用 - Why does my java guessing game not work after the first loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM