List接口
特点:
1.它是一个元素存取有序的集合。例如,存元素的顺序是11、22、33。那么集合中,元素的存储就是按照11、22、33的顺序完成的)。
2.它是一个带有索引的集合,通过索引就可以精确的操作集合中的元素(与数组的索引是一个道理)。
3.集合中可以有重复的元素,通过元素的equals方法,来比较是否为重复的元素
List接口中常用方法
方法名 | 说明 |
---|---|
public void add(int index, E element) | 将指定的元素,添加到该集合中的指定位置上。 |
public E get(int index) | 返回集合中指定位置的元素 |
public E remove(int index) | 移除列表中指定位置的元素, 返回的是被移除的元素。 |
remove(Object o) | 删除对象, |
public E set(int index, E element) | 用指定元素替换集合中指定位置的元素,返回值的更新前的元素。 |
ArrayList集合
java.util.ArrayList 集合数据存储的结构是数组结构。元素增删慢,查找快,由于日常开发中使用最多的功能为查询数据、遍历数据,所以 ArrayList 是最常用的集合。
案例1
代码实现:
·· 学生类
package Day0816am;
public class Student {
private String name;
private double score;
public Student() {
}
public Student(String name, double score) {
super();
this.name = name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
@Override
public String toString() {
return "Student [name=" + name + ", score=" + score + "]";
}
}
··· 测试类
@Test
public void test1(){
ArrayList<Student> stu = new ArrayList<Student>();
stu.add(new Student("张三",98.0));
stu.add(new Student("李四",98.0));
stu.add(new Student("王五",98.0));
double sum = 0.0;
double avg = 0.0;
//增强for循环
System.out.println("增强for循环");
for(Student s:stu){
System.out.println(s);
}
Iterator it=stu.iterator();
//
//用while循环改进元素的判断和获取
System.out.println("迭代器");
while (it.hasNext()) {
Student s = (Student) it.next();
sum += s.getScore();
System.out.println(s);
}
System.out.println("总成绩为:"+sum);
System.out.println("总成绩为:"+sum/stu.size());
}
案例2
功能描述:
使用ArrayList保存学生信息(学号,姓名,年龄,成绩),要求删除指定学生后遍历输出。
思路分析:
1、创建三个学生对象保存到集合
2、删除一个对象
3、遍历集合
— 学生类
package Day0816am;
public class Student {
private String name;
private double score;
private String id;
private int age;
public Student() {
}
public Student(String name, double score, String id, int age) {
super();
this.name = name;
this.score = score;
this.id = id;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student [name=" + name + ", score=" + score + ", id=" + id + ", age=" + age + "]";
}
}
— 测试类
public void test3(){
ArrayList<Student> stu = new ArrayList<Student>();
stu.add(new Student("张三",98.0,"001213",18));
stu.add(new Student("李四",97.0,"565956",19));
stu.add(new Student("王五",88.0,"365459",65));
for(Student s : stu){
System.out.println(s);
}
//删除,通过索引删除
System.out.println("删除元素后");
stu.remove(1);
for(Student s : stu){
System.out.println(s);
}
/*
运行结果:
Student [name=张三, score=98.0, id=001213, age=18]
Student [name=李四, score=97.0, id=565956, age=19]
Student [name=王五, score=88.0, id=365459, age=65]
删除元素后
Student [name=张三, score=98.0, id=001213, age=18]
Student [name=王五, score=88.0, id=365459, age=65]
*/
}
LinkedList集合
java.util.LinkedList 集合数据存储的结构是链表结构。方便元素添加、删除的集合。
方法名 | 说明 |
---|---|
public void addFirst(E e) | 在该列表开头插入指定的元素 |
public void addLast(E e) | 将指定的元素追加到此列表的末尾 |
public E getFirst() | 返回此列表中的第一个元素 |
public E getLast() | 返回此列表中的最后一个元素 |
public E removeFirst() | 从此列表中删除并返回第一个元素 |
public E removeLast() | 从此列表中删除并返回最后一个元素 |
public E pop() | 从此列表所表示的堆栈处弹出一个元素。 |
public void push(E e) | 将元素推入此列表所表示的堆栈。 |
public boolean isEmpty() | 如果列表不包含元素,则返回true。 |