hasnext() 如何在 Java 集合中工作

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

how hasnext() works in collection in java

javalistclasscollectionspackage

提问by Vinoth Kumar

program:

程序:

public class SortedSet1 {

  public static void main(String[] args) {  

    List ac= new ArrayList();

    c.add(ac);
    ac.add(0,"hai");
    ac.add(1,"hw");
    ac.add(2,"ai"); 
    ac.add(3,"hi"); 
    ac.add("hai");

    Collections.sort(ac);

    Iterator it=ac.iterator();

    k=0;

    while(it.hasNext()) {    
      System.out.println(""+ac.get(k));
      k++;     
    }
  }
}

output: ai hai hi hw hai

输出:ai hai hi hw hai

how it execute 5 times?? while come to hai no next element present so condition false. But how it executed.

它如何执行 5 次?虽然来海没有下一个元素出现所以条件错误。但它是如何执行的。

回答by Péter T?r?k

Your loop above iterates through the list using an index. it.hasNext()returns true until itreaches the end of the list. Since you don't call it.next()within your loop to advance the iterator, it.hasNext()keeps returning true, and your loop rolls on. Until, that is, kgets to be 5, at which point an IndexOutOfBoundsExceptionis thrown, which exits the loop.

上面的循环使用索引遍历列表。it.hasNext()返回 true 直到it到达列表的末尾。由于您没有it.next()在循环中调用来推进迭代器,因此it.hasNext()不断返回 true,并且您的循环继续进行。直到,即k得到 5,此时IndexOutOfBoundsException抛出an ,退出循环。

The proper idiom using an iterator would be

使用迭代器的正确习惯用法是

while(it.hasNext()){
    System.out.println(it.next());
}

or using an index

或使用索引

for(int k=0; k<ac.size(); k++) {
  System.out.println(ac.get(k));
}

However since Java5, the preferred way is using the foreach loop(and generics):

但是,从 Java5 开始,首选方法是使用foreach 循环(和generics):

List<String> ac= new ArrayList<String>();
...
for(String elem : ac){
    System.out.println(elem);
}

回答by Xavier Combelle

the point is ac.get(k) doesn't consume any element of the iterator at the contrary of it.next()

关键是 ac.get(k) 不消耗迭代器的任何元素,与 it.next() 相反

回答by locka

That loop will never terminate. it.hasNext does not advance the iterator. You have to call it.next() to advance it. The loop probably terminates because k becomes 5 at which point the Arraylist with throw a bounds exception.

该循环永远不会终止。it.hasNext 不推进迭代器。你必须调用 it.next() 来推进它。循环可能会终止,因为 k 变为 5,此时 Arraylist 抛出边界异常。

The correct form of iterating a list (containing strings) is either:

迭代列表(包含字符串)的正确形式是:

Iterator it = ac.iterator();
while (it.hasNext) {
  System.out.println((String) it.next());
}

Or if the list is typed, e.g. ArrayList

或者如果列表是键入的,例如 ArrayList

for (String s : ac) {
  System.out.println((String) s);
}

Or if you absolutely know this is an array list and need speed over terseness:

或者,如果您完全知道这是一个数组列表并且需要速度而不是简洁:

for (int i = 0; i < ac.size(); i++) {
  System.out.println(ac.get(i));
}