黑马程序员技术交流社区

标题: List [打印本页]

作者: 122125241    时间: 2015-7-4 08:14
标题: List


  1. List具备父类Collection的所有方法,同时List有自己专用的列表迭代器,拥有父类遍历的同时,可以逆向遍历,但也有缺点就是需正向遍历以后才能使用逆向遍历

  2. List的遍历功能,可以使用父类的Iterator遍历方法外,还有集合自身遍历元素 不需要迭代器参与
  3. List list = new ArrayList();
  4.         // 创建学生对象
  5. Student s1 = new Student("林黛玉", 18);
  6.         Student s2 = new Student("刘姥姥", 88);
  7.         Student s3 = new Student("王熙凤", 38);
  8.         // 把学生添加到集合中
  9. list.add(s1);
  10.         list.add(s2);
  11.         list.add(s3);
  12.         // 遍历
  13. // 迭代器遍历
  14. Iterator it = list.iterator();
  15.         while (it.hasNext()) {
  16.         Student s = (Student) it.next();
  17.         System.out.println(s.getName() + "---" + s.getAge());
  18.         }
  19.         System.out.println("--------");
  20.         // 普通for循环.集合自身遍历
  21. for (int x = 0; x < list.size(); x++) {
  22.         Student s = (Student) list.get(x);
  23.         System.out.println(s.getName() + "---" + s.getAge());
  24.         }
  25. List子类ListIterdtor的逆向遍历
  26. Iterator it = list.iterator();//正向遍历一个字符串对象集合
  27. while (it.hasNext()) {
  28.         String s = (String) it.next();
  29.         System.out.println(s);
  30.         }
  31.         System.out.println("-----------------");

  32. ListIterator lit = list.listIterator();//调用特有方法(ListIterator继承了Iterator)
  33.         while (lit.hasPrevious()) { //然后使用List特
  34. String s = (String) lit.previous();
  35.         System.out.println(s);
  36.         }
  37.         System.out.println("-----------------");



复制代码



作者: 徐会会    时间: 2015-7-4 08:19
非常给力
作者: micro_hx    时间: 2015-7-4 08:41
很好。支持一下啊。。。。




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2