黑马程序员技术交流社区
标题:
List
[打印本页]
作者:
122125241
时间:
2015-7-4 08:14
标题:
List
List具备父类Collection的所有方法,同时List有自己专用的列表迭代器,拥有父类遍历的同时,可以逆向遍历,但也有缺点就是需正向遍历以后才能使用逆向遍历
List的遍历功能,可以使用父类的Iterator遍历方法外,还有集合自身遍历元素 不需要迭代器参与
List list = new ArrayList();
// 创建学生对象
Student s1 = new Student("林黛玉", 18);
Student s2 = new Student("刘姥姥", 88);
Student s3 = new Student("王熙凤", 38);
// 把学生添加到集合中
list.add(s1);
list.add(s2);
list.add(s3);
// 遍历
// 迭代器遍历
Iterator it = list.iterator();
while (it.hasNext()) {
Student s = (Student) it.next();
System.out.println(s.getName() + "---" + s.getAge());
}
System.out.println("--------");
// 普通for循环.集合自身遍历
for (int x = 0; x < list.size(); x++) {
Student s = (Student) list.get(x);
System.out.println(s.getName() + "---" + s.getAge());
}
List子类ListIterdtor的逆向遍历
Iterator it = list.iterator();//正向遍历一个字符串对象集合
while (it.hasNext()) {
String s = (String) it.next();
System.out.println(s);
}
System.out.println("-----------------");
ListIterator lit = list.listIterator();//调用特有方法(ListIterator继承了Iterator)
while (lit.hasPrevious()) { //然后使用List特
String s = (String) lit.previous();
System.out.println(s);
}
System.out.println("-----------------");
复制代码
作者:
徐会会
时间:
2015-7-4 08:19
非常给力
作者:
micro_hx
时间:
2015-7-4 08:41
很好。支持一下啊。。。。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2