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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 122125241 中级黑马   /  2015-7-4 08:14  /  343 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文



  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("-----------------");



复制代码


2 个回复

倒序浏览
非常给力
回复 使用道具 举报
很好。支持一下啊。。。。
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马