Arraylist对象是可变数组,可以不设置长度。下面介绍一些常用方法。该对象是从0下标开始计算的。主要分成四个部分,增加元素,删除元素,修改元素,查找元素。
每个
ArrayList
实例都有一个
容量
。该容量是指用来存储列表元素的数组的大小。它总是至少
等于列表的大小。随着向
ArrayList
中不断添加元素,其容量也自动增长。
1. 创建ArrayList
ArrayList<Object> list1 = new ArrayList<>();
ArrayList<Object> list2 = new ArrayList<>(10);
ArrayList<Object> list3 = new ArrayList<>(list2);
ArrayList的扩容底层调用了native方法System.arraycopy()简单点说就是将原来的数组中的元素拷贝到一个新的更大的数组中去。
2 初始化指定集合大小ArrayList
我们可以在创建ArrayList集合的时候指定其处理容量,看下源码:
/**
* Constructs an empty list with the specified initial capacity.
*
* @param initialCapacity the initial capacity of the list
* @throws IllegalArgumentException if the specified initial capacity
* is negative
*/
public ArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
3 初始化传递集合ArrayList
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
if ((size = elementData.length) != 0) {
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
} else {
// replace with empty array.
this.elementData = EMPTY_ELEMENTDATA;
}
}
4 添加元素
1、直接在list末尾添加元素:list.add();
2、用于在列表的指定位置插入指定元素,并将当前处于该位置的元素及其后续元素的索引加1:void add(int index, E element)。
import java.util.ArrayList;
class Main {
public static void main(String[] args) {
//创建一个数组
ArrayList<Integer> primeNumbers = new ArrayList<>();
// 往数组插入元素
primeNumbers.add(2);
primeNumbers.add(3);
primeNumbers.add(5);
System.out.println("ArrayList: " + primeNumbers);
}
}
5 添加到指定.位置add
arraylist里面首先会确认容量是否够用,如果不够,则进行扩容。注意ArrayList的扩容时机只有底层数组已满,不能放下即将存入的对象才会扩容。
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
public void add(int index, E element) {
//检查index是否在已有的数组中
if (index > size || index < 0)
throw new IndexOutOfBoundsException("Index:"+index+",Size:"+size);
ensureCapacity(size + 1);//确保对象数组elementData有足够的容量,可以将新加入的元素e加进去
System.arraycopy(elementData, index, elementData, index+1, size-index);//将index及其后边的所有的元素整块后移,空出index位置
elementData[index] = element;//插入元素
size++;//已有数组元素个数+1
}
6 删除元素
(1) 删除指定索引元素 E remove(int index)。
public E remove(int index) {
rangeCheck(index);
modCount++;
//取得被删除元素
E oldValue = elementData(index);
int numMoved = size - index - 1;
if (numMoved > 0)
//如果删除的不是最后一个元素,进行数组拷贝
System.arraycopy(elementData, index+1, elementData, index,
numMoved);
elementData[--size] = null; // clear to let GC do its work
//返回被删除的值
return oldValue;
}
(2)删除指定值的元素 remove(Object o)
public boolean remove(Object o) {
if (o == null) {//移除对象数组elementData中的第一个null
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {//移除对象数组elementData中的第一个o
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
7 获取元素
获取单个元素get(int index)
public E get(int index) {
rangeCheck(index);//检查索引范围
return (E) elementData[index];//返回元素,并将Object转型为E
}
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}