集合
-
概述
面向对象语言对事物的体现都是以对象的形式体现,所以为了方便对多个对象的操作, Java就提供了集合类。
-
数组和集合的区别
2.1 长度的区别:
数组的长度是固定的,而集合的长度是可变的
2.2 存储类型的区别:
数组可以存储基本数据类型,也可以存储引用数据类型;而集合只能存储引用数据类型
2.3 内容区别;
数组只能存储同种数据元素,集合可以存储不同类型的元素
Collection集合
-
功能概述
boolean add(object obj) : 添加一个元素
boolean addAll(Collection c):添加一个集合的元素 (给一个集合添加进另一个集合中的所有元素
void clear():移除所有元素
boolean remove(Object o):移除一个元素
boolean removeAll(Collection c):移除一个集合的元素(移除一个以上返回的就是true) 删除的元素是两个集合的交集元素 ,如果没有交集元素 则删除失败 返回false
boolean contains(Object o):判断集合中是否包含指定的元素
boolean containsAll(Collection c):判断集合中是否包含指定的集合元素(这个集合 包含 另一个集合中所有的元素才算包含 才返回true)
Object[] toArray():把集合转换为数组
-
把集合转成数组toArray(),遍历这个数组 可以实现集合的遍历
public class JiHeBiJi {
public static void main(String[] args) {
Collection<Integer> list = new ArrayList<>();
list.add(100);
list.add(200);
list.add(300);
list.add(400);
Object[] array = list.toArray();
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
- Collection存储自定义对象并把集合转成对象数组遍历
public class JiHeBiJi {
public static void main(String[] args) {
Collection list = new ArrayList();
Student s1 = new Student("张三", 23);
Student s2 = new Student("李四", 24);
Student s3 = new Student("王五", 25);
list.add(s1);
list.add(s2);
list.add(s3);
Object[] array = list.toArray();
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
}
}
- Collection存储自定义对象并把集合使用迭代器遍历
public class JiHeBiJi {
public static void main(String[] args) {
Collection list = new ArrayList();
Student s1 = new Student("张三", 23);
Student s2 = new Student("李四", 24);
Student s3 = new Student("王五", 25);
list.add(s1);
list.add(s2);
list.add(s3);
Iterator iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
-
为什么迭代器不定义成一个类 , 而是定义成一个接口?
假设迭代器定义为一个类,首先若是具体类那么就会提供一个公共的实现不同集合遍历的方法,我们知道这是不可能的。
若是一个抽象类又因为java中类是单继承的,所以无法同时继承多个类。而且集合的根接口无法继承抽象类
其实迭代器的具体实现方法是在集合的具体子类中以内部类的方式实现的
List集合
-
概述:
元素有序,并且每一个元素都存在一个索引,元素可以重复
-
案例演示
List集合存储字符串并遍历
迭代器的方式
public class ListDemo {
public static void main(String[] args) {
List<String> list = new ArrayList<>();
list.add("aaa");
list.add("bbb");
list.add("nnn");
list.add("mmm");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
- List集合存储学生对象并遍历
迭代器的方式
public class ListDemo {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
Student s1 = new Student("张三", 23);
Student s2 = new Student("李四", 24);
Student s3 = new Student("王五", 25);
list.add(s1);
list.add(s2);
list.add(s3);
Iterator<Student> iterator = list.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}
}
-
List集合特有的功能
void add(int index,E element): 在指定索引处添加元素
E remove(int index):移除指定索引处的元素 返回的是移除的元素
E get(int index):获取指定索引处的元素
E set(int index,E element):更改指定索引处的元素 返回的而是被替换的元素
int indexOf(Object o): 返回此列表中第一次出现的指定元素的索引;如果此列表不包含该元素,则返回 -1。
int lastIndexOf(Object o) :返回此列表中最后出现的指定元素的索引;如果列表不包含此元素,则返回 -1。
-
案例演示: 使用for循环 通过size()和get()方法结合使用遍历。
public class ListDemo {
public static void main(String[] args) {
List<Student> list = new ArrayList<>();
Student s1 = new Student("张三", 23);
Student s2 = new Student("李四", 24);
Student s3 = new Student("王五", 25);
list.add(s1);
list.add(s2);
list.add(s3);
for (int i = 0; i < list.size(); i++) {
Student student = list.get(i);
System.out.println(student);
}
}
}
-
并发修改异常产生的原因及解决方案
ConcurrentModificationException出现:
我们用Iterator这个迭代器遍历采用hasNext方法和next方法,集合修改集合 会出现并发修改异常
原因是我们的迭代依赖与集合,当我们往集合中添加好了元素之后,获取迭代器 ,那么迭代器已经知道了集合的元素个数。这个时候你在遍历的时候又突然想给 集合里面加一个元素(用的是集合的add方法),那迭代器不同意 就报错了
解决方案
我们用ListIterator迭代器遍历 用迭代器自带的add方法添加元素 那就不会报错了
a:迭代器迭代元素,迭代器修改元素(ListIterator的特有功能add) b:集合遍历元素,集合修改元素
解决方案2 使用for循环遍历集合 添加元素 不会报错
-
List的三个子类的特点
ArrayList:
底层数据结构是数组,查询快,增删慢。
线程不安全,效率高
Vector:
底层数据结构是数组,查询快,增删慢。
线程安全,效率低
LinkedList:
底层数据结构是链表,查询慢,增删块。
线程不安全,效率高。
本文详细介绍了Java集合框架中的核心概念,包括数组与集合的区别、Collection接口的基本操作,如添加、删除、遍历元素。同时,展示了List接口的特点,如有序、可重复性,以及特有的索引操作。此外,还探讨了并发修改异常的产生原因和解决方案,并列举了ArrayList、Vector和LinkedList三个List子类的特性。最后,讨论了如何避免并发修改异常以及List子类的选择依据。
1157

被折叠的 条评论
为什么被折叠?



