Java 中的ArrayList常用方法总结
在Java中,ArrayList 是 java.util 包下的一个动态数组实现,属于 Collection 框架的一部分。它允许存储任意数量的对象,并且可以根据需要自动调整大小。ArrayList 提供了许多常用的方法来操作其内部的数据。以下是 ArrayList 中一些常用的方法及其详细解释:
1. 构造方法
无参构造方法
ArrayList<E> list = new ArrayList<>()
指定初始容量的构造方法
ArrayList<E> list = new ArrayList<>(20
创建一个空的 `ArrayList`,初始容量为20
基于集合的构造方法
Collection<? extends E> c = ...;
ArrayList<E> list = new ArrayList<>(c)
创建一个包含指定集合元素的 ArrayList,初始容量为集合大小。
2. 常用方法
添加元素
add(E e)
list.add("元素1");
将指定的元素添加到列表的末尾。
add(int index, E element)
list.add(1, "元素2");
在指定的位置插入指定的元素。当前在该位置及其之后的所有元素都将向后移动一个位置。
addAll(Collection<? extends E> c)
Collection<String> collection = Arrays.asList("元素3", "元素4");
list.addAll(collection);
将指定集合中的所有元素添加到列表的末尾。
addAll(int index, Collection<? extends E> c)
list.addAll(2, collection);
从指定的位置开始,将指定集合中的所有元素插入到列表中。
获取元素
get(int index)
String element = list.get(0);
返回列表中指定位置的元素。
修改元素
set(int index, E element)
list.set(0, "新元素");
用指定的元素替换列表中指定位置的元素。
删除元素
remove(int index)
list.remove("元素2");
移除列表中第一个出现的指定元素(如果存在)。
removeAll(Collection<?> c)
list.removeAll(collection);
移除列表中所有包含在指定集合中的元素。
retainAll(Collection<?> c)
list.retainAll(collection);
仅保留列表中包含在指定集合中的元素。
clear()
list.clear();
移除列表中的所有元素。
查询元素
contains(Object o)
boolean exists = list.contains("元素1");
判断列表中是否包含指定的元素。
indexOf(Object o)
int index = list.indexOf("元素1");
返回列表中第一次出现的指定元素的索引,如果未找到则返回-1。
lastIndexOf(Object o)
int lastIndex = list.lastIndexOf("元素1");
返回列表中最后一次出现的指定元素的索引,如果未找到则返回-1。
isEmpty()
boolean empty = list.isEmpty();
判断列表是否为空。
size()
int size = list.size();
返回列表中元素的数量。
遍历列表
使用增强型 for 循环
for (String element : list) {
System.out.println(element);
}
使用 Iterator
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
}
使用 forEach 方法(Java 8+)
list.forEach(element -> System.out.println(element));
其他常用方法
subList(int fromIndex, int toIndex)
List<String> subList = list.subList(1, 3);
返回列表中从 fromIndex 到 toIndex(不包括 toIndex)的子列表。
toArray()
Object[] array = list.toArray();
返回一个包含列表中所有元素的数组。
toArray(T[] a)
String[] array = list.toArray(new String[0]);
返回一个包含列表中所有元素的数组,数组类型为指定类型。
示例代码
import java.util.ArrayList;
import java.util.Iterator;
public class ArrayListExample {
public static void main(String[] args) {
// 创建一个 ArrayList
ArrayList<String> list = new ArrayList<>();
// 添加元素
list.add("苹果");
list.add("香蕉");
list.add("橙子");
System.out.println("添加元素后: " + list);
// 在指定位置添加元素
list.add(1, "葡萄");
System.out.println("在索引1添加葡萄后: " + list);
// 获取元素
String fruit = list.get(2);
System.out.println("索引2的元素: " + fruit);
// 修改元素
list.set(0, "西瓜");
System.out.println("修改索引0的元素后: " + list);
// 删除元素
list.remove(3);
System.out.println("删除索引3的元素后: " + list);
// 查找元素
boolean contains = list.contains("香蕉");
System.out.println("是否包含香蕉: " + contains);
// 遍历列表
System.out.println("遍历列表:");
for (String element : list) {
System.out.println(element);
}
// 使用 Iterator 遍历
System.out.println("使用 Iterator 遍历:");
Iterator<String> iterator = list.iterator();
while (iterator.hasNext()) {
String element = iterator.next();
System.out.println(element);
}
// 使用 forEach 方法遍历
System.out.println("使用 forEach 方法遍历:");
list.forEach(element -> System.out.println(element));
}
}