package com.xawl.demo3;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
/**
* @Author:liger
* @Description:爱学Java
*/
public class Test03 {
public static void main(String[] args) {
List list = new ArrayList();
list.add(new Student("张三",20));
list.add(new Student("李四",30));
list.add(new Student("王五",40));
//用list迭代器遍历list集合
ListIterator listIterator = list.listIterator();
while (listIterator.hasNext()) {
Student next = (Student) listIterator.next();//向下转型
System.out.println(next.getName()+"=="+next.getAge());
}
System.out.println("*******我是分隔符********");
//list的反向迭代
while (listIterator.hasPrevious()) {
Student previous = (Student) listIterator.previous();//向下转型
System.out.println(previous.getName()+"=="+previous.getAge());
}
System.out.println("*******我是分隔符********");
//for循环遍历
for (int i = 0; i < list.size(); i++) {
Student o = (Student) list.get(i);
System.out.println(o.getName()+"=="+o.getAge());
}
}
}
Student类
package com.xawl.demo3;
class Student{
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}