A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© chudaming213 中级黑马   /  2014-12-14 16:13  /  755 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

各位快过来看看啦,有问题啦有问题啦!!
问题:请问使用迭代器遍历了list之后,还能有什么办法
  1. import java.util.Iterator;
  2. import java.util.LinkedList;

  3. public class LinkedListDemo {
  4.         public static void main(String[] args) {
  5.                 LinkedList li = new LinkedList();
  6.                 li.add(new Person("wangwu0", 25));
  7.                 li.add(new Person("wangwu32", 65));
  8.                 li.add(new Person("wangwu4", 34));
  9.                 li.add(new Person("wangwu3", 25));
  10.                 li.add(new Person("wangwu3", 25));
  11.                 Iterator it = li.iterator();
  12.                 while (it.hasNext())
  13.                         System.out.println(it.next());
  14.                 System.out.println();
  15. System.out.println(li.remove(2));
  16.                 System.out.println();
  17.                 while (it.hasNext()) //这里没有效果的。想问问有没有用构造器再实现一次的方案
  18.                         System.out.println(it.next());
  19.         }

  20. }
复制代码

再使用这个迭代器在遍历一遍list吗?小弟写了个代码想先输出一遍,去掉某些元素后在遍历一遍。
  1.          for (int i = 0; i < li.size(); i++) {
  2.                  System.out.println(li.get(i));
  3.                  }
  4.                  System.out.println(li.getFirst());
复制代码
后来我把代码改成上面的,想利用索引解决问题,但是出现了意想不到的结果:
Person [name=wangwu0, age=25]
Person [name=wangwu32, age=65]
Person [name=wangwu3, age=25]
Person [name=wangwu3, age=25]
Person [name=wangwu0, age=25]
以上只是for循环输出的部分

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马