1. List集合常用子类:
ArrayList:底层数据结构是数组,查询快,增删慢
LinkedList:底层数据结构是链表,查询慢,增删快
2. 应用案例:ArrayList集合存储学生对象用三种方式遍历
需求:创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合
思路:
- 定义学生类
- 创建ArrayList集合对象
- 创建学生对象
- 把学生添加到集合
- 遍历集合
迭代器:集合特有的遍历方式
普通for:带有索引的遍历方式
增强for:最方便的遍历方式
学生类:
public 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;
}
}
测试类:
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListDemo {
public static void main(String[] args) {
//创建ArrayList集合对象
ArrayList<Student> array = new ArrayList<Student>();
//创建学生对象
Student s1 = new Student("林青霞", 30);
Student s2 = new Student("张曼玉", 35);
Student s3 = new Student("王祖贤", 33);
//把学生添加到集合
array.add(s1);
array.add(s2);
array.add(s3);
//迭代器:集合特有的遍历方式
Iterator<Student> it = array.iterator();
while (it.hasNext()) {
Student s = it.next();
System.out.println(s.getName() + "," + s.getAge());
}
System.out.println("--------");
//普通for:带有索引的遍历方式
for(int i=0; i<array.size(); i++) {
Student s = array.get(i);
System.out.println(s.getName() + "," + s.getAge());
}
System.out.println("--------");
//增强for:最方便的遍历方式
for(Student s : array) {
System.out.println(s.getName() + "," + s.getAge());
}
}
}
3. LinkedList集合的特有功能
注:大家可以尝试用LinkedList方式完成上面案例的功能。