1. 1 List的特点
1.2 存储学生对象并遍历
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ListDemo {
public static void main(String[] args) {
List<Student> students = new ArrayList<Student>();
Student jack = new Student("jack", 22);
Student rose = new Student("rose", 20);
Student lili = new Student("lili", 19);
students.add(jack);
students.add(rose);
students.add(lili);
System.out.println(students);
System.out.println("-----------");
Student gg = new Student("gg", 21);
students.set(2,gg);
System.out.println(students.get(1));
System.out.println("--------");
Iterator<Student> its = students.iterator();
while (its.hasNext()) {
Student stu = its.next();
System.out.println(stu.getName()+ " " + stu.getAge());
}
System.out.println("-----------");
for (int i=0; i<students.size(); i++) {
Student stu2 = students.get(i);
System.out.println(stu2.getName()+ " " + stu2.getAge());
}
students.remove(2);
System.out.println(students);
}
}