8. ArrayList 集合
8.1 为什么要有集合
-
数组的局限性:数组长度固定,存储数据类型单一(只能存基本类型或引用类型,但一旦声明类型就不能改变),增删元素时需要手动操作索引,灵活性较差
-
集合的优势:集合(如 ArrayList)长度可变,能动态添加 / 删除元素,且通过泛型可约束存储数据类型,更适合处理不确定数据量的场景
8.2 集合概述
-
定义:集合是一种存储空间可变的存储模型,用于存储数据,容量可动态改变
-
分类:Java 集合分为单列集合(如 List、Set)和双列集合(如 Map),ArrayList 属于单列集合中的 List 体系
-
特点:
- 长度可变,底层基于数组实现,自动扩容。
- 只能存储引用数据类型(如 String、自定义对象),若存基本类型需用包装类(如 Integer、Double)
8.3 ArrayList 类详解
8.3.1 泛型的使用
-
作用:约束集合中存储元素的数据类型,避免类型错误,提升代码安全性和可读性
-
语法:
ArrayList<数据类型>
-
例子:
ArrayList<String> strList = new ArrayList<>(); // 存储字符串
ArrayList<Integer> numList = new ArrayList<>(); // 存储整数(自动装箱)
8.3.2 构造方法
方法名 | 说明 |
---|---|
public ArrayList() | 创建一个空的集合对象 |
例子:
ArrayList<String> list = new ArrayList<>(); // 创建空集合
8.3.3 常用成员方法
方法名 | 说明 | 返回值 / 参数 |
---|---|---|
public boolean add(E e) | 将元素添加到集合末尾 | boolean(成功为 true) |
public E remove(int index) | 删除指定索引处的元素 | 被删除的元素 |
public boolean remove(Object o) | 删除指定元素(按值删除) | boolean(成功为 true) |
public E set(int index, E element) | 修改指定索引处的元素 | 被修改的旧元素 |
public E get(int index) | 获取指定索引处的元素 | 对应索引的元素 |
public int size() | 获取集合中元素的个数 | int |
例子:
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
// 添加元素
list.add("Apple");
list.add("Banana");
System.out.println("添加后集合:" + list); // 输出:[Apple, Banana]
// 删除元素(按索引)
String removed = list.remove(0);
System.out.println("删除的元素:" + removed); // 输出:Apple
System.out.println("删除后集合:" + list); // 输出:[Banana]
// 删除元素(按值)
boolean result = list.remove("Banana");
System.out.println("是否删除成功:" + result); // 输出:true
// 添加多个元素并修改
list.add("Cat");
list.add("Dog");
list.set(1, "Elephant"); // 将索引 1 的元素改为 Elephant
System.out.println("修改后集合:" + list); // 输出:[Cat, Elephant]
// 获取元素
String first = list.get(0);
System.out.println("第一个元素:" + first); // 输出:Cat
// 获取集合长度
int size = list.size();
System.out.println("集合长度:" + size); // 输出:2
}
}
8.4 ArrayList 存储数据类型及遍历
8.4.1 存储字符串并遍历
- 例子:创建存储字符串的集合,添加元素后遍历输出
public class ArrayListStringDemo {
public static void main(String[] args) {
ArrayList<String> strList = new ArrayList<>();
strList.add("Hello");
strList.add("World");
strList.add("Java");
// 遍历方式 1:for 循环(推荐)
System.out.println("遍历结果(for 循环):");
for (int i = 0; i < strList.size(); i++) {
System.out.print(strList.get(i) + " "); // 输出:Hello World Java
}
// 遍历方式 2:增强 for 循环(JDK 1.5+)
System.out.println("\n遍历结果(增强 for):");
for (String s : strList) {
System.out.print(s + " "); // 输出:Hello World Java
}
}
}
8.4.2 存储自定义对象并遍历
-
例子:学生类
- 学生类定义:
public class Student {
private String name;
private int age;
// 构造方法、getter 和 setter 方法
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() { return name; }
public int getAge() { return age; }
}
- 遍历代码:
public class ArrayListStudentDemo {
public static void main(String[] args) {
ArrayList<Student> stuList = new ArrayList<>();
stuList.add(new Student("张三", 18));
stuList.add(new Student("李四", 19));
// 遍历集合,输出学生信息
System.out.println("学生列表:");
for (int i = 0; i < stuList.size(); i++) {
Student stu = stuList.get(i);
System.out.println("姓名:" + stu.getName() + ",年龄:" + stu.getAge());
}
}
}
8.5 常见操作案例
8.5.1 根据元素值查找索引
- 例子:在集合中查找指定元素的索引,存在返回索引,不存在返回 -1
public class FindIndexDemo {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("A");
list.add("B");
list.add("C");
int index = findIndex(list, "B");
System.out.println("元素 B 的索引:" + index); // 输出:1
int notExistIndex = findIndex(list, "D");
System.out.println("元素 D 的索引:" + notExistIndex); // 输出:-1
}
// 查找索引的方法
public static int findIndex(ArrayList<String> list, String target) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i).equals(target)) {
return i; // 找到即返回索引
}
}
return -1; // 未找到返回 -1
}
}
8.5.2 判断元素是否存在
- 例子:判断集合中是否包含指定元素,存在返回
true
,否则返回false
public class ContainsDemo {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<>();
nums.add(10);
nums.add(20);
nums.add(30);
boolean has20 = contains(nums, 20);
System.out.println("包含 20 吗?" + has20); // 输出:true
boolean has40 = contains(nums, 40);
System.out.println("包含 40 吗?" + has40); // 输出:false
}
// 判断是否存在的方法
public static boolean contains(ArrayList<Integer> list, int num) {
for (int n : list) { // 增强 for 循环遍历
if (n == num) {
return true;
}
}
return false;
}
}
8.6 注意事项
-
泛型只能用引用类型:不能写基本类型(如
int
),需用包装类(如Integer
) -
索引越界异常:操作索引时需确保索引在
0 ~ size()-1
范围内,否则抛出IndexOutOfBoundsException
-
集合扩容机制:ArrayList 底层数组默认初始容量为 10,当元素满时,会自动扩容为原容量的 1.5 倍(性能消耗较低)