(1)
ArrayList arr3 = new ArrayList();
  arr3.add("abc");
  arr3.add("zwj");
  arr3.add(new Date());
  while(arr3.iterator().hasNext()){
  System.out.print(arr3.iterator().next());
这段代码好像有点问题的,我运行过后,它一直输出abc,停都停不下来,这到底是怎么一回事啊??
而我用下面这段代码就可以的
ArrayList arr3 = new ArrayList(); 
      arr3.add("abc");
      arr3.add("zwj");
      arr3.add(new Date());
      Iterator it=arr3.iterator(); //使用迭代器
      while(it.hasNext()){
       Object o=it.next();
         System.out.print(o + " ");
      }而这种方法就可以的,这是什么原因呢?(2)如何编写程序对LinkedList链表进行插入和删除操作呢?

解决方案 »

  1.   

    while(arr3.iterator().hasNext()) 这一句有问题, arr3.iterator()每次都会创建一个新的迭代器, 所以死循环了. 看一下ArrayList的源代码就更加清楚了.
      

  2.   

    查看j2se的API文档,上面有实现的方法。
      

  3.   

    while(arr3.iterator().hasNext()) 
    while每循环一次,就运行arr3.iterator().hasNext()一次,
    arr3.iterator()每次都会重新创建一个新的集合,
    所以你每次是新创建的arr3.iterator()的第一个子集。
    所以当然一样一直输出abc了。