ArrayList类
ArrayList特点:
数据重复性:数据可重复
数据有序性:保证插入数据的有序性
null值问题:可以有多个null值
底层数据结构:数组(自增长)
继承关系:
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable{}
ArrayList继承AbstractList类,实现了List接口,支持快速随机访问,可以克隆,可以进行序列化和反序列化操作。
基本属性:
private static final long serialVersionUID = 8683452581122892189L;//用于Java的序列化机制
private static final int DEFAULT_CAPACITY = 10;//数组的默认初始容量
private static final Object[] EMPTY_ELEMENTDATA = {};//数组的初始化使用
private transient Object[] elementData;//ArrayList的底层数据结构是数组
private int size;//集合中存储元素的个数
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;//数组长度的最大值
protected transient int modCount = 0;//表示的是版本控制,添加,删除,修改之类的操作都会进行++操作
构造函数:
//定义初始容量的构造方法,如果初始容量大于0,则得到一个大小是初始容量的Object数组
public ArrayList(int initialCapacity) {
super();
if (initialCapacity < 0)
throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity);
this.elementData = new Object[initialCapacity];
}
//无参构造方法,得到一个空数组
public ArrayList() {
super();
this.elementData = EMPTY_ELEMENTDATA;
}
//使用集合的构造方法,ArrayList中的数组将集合中所有元素拷贝一份,如果得到的数组不是Object数组,将转化为Object数组
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
默认值:
private static final int DEFAULT_CAPACITY = 10;//数组的默认初始容量
默认数组初始化大小为10。
增长方式:
private void grow(int minCapacity) {
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);//新数组长度为原数组的1.5倍
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
elementData = Arrays.copyOf(elementData, newCapacity);
}
数组是1.5倍扩容。
主要实现方法:
//返回此列表中的元素数
public int size() {
return size;
}
//判断此列表是否为空
public boolean isEmpty() {
return size == 0;
}
//判断此列表中是否包含指定的元素
public boolean contains(Object o) {
return indexOf(o) >= 0;
}
//搜索给定参数第一次出现的位置
public int indexOf(Object o) {
if (o == null) {
for (int i = 0; i < size; i++)
if (elementData[i]==null)
return i;
} else {
for (int i = 0; i < size; i++)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
//返回指定的对象在列表中最后一次出现的位置
public int lastIndexOf(Object o) {
if (o == null) {
for (int i = size-1; i >= 0; i--)
if (elementData[i]==null)
return i;
} else {
for (int i = size-1; i >= 0; i--)
if (o.equals(elementData[i]))
return i;
}
return -1;
}
//返回此 ArrayList 实例的浅表复制
public Object clone() {
try {
ArrayList<E> v = (ArrayList<E>) super.clone();
v.elementData = Arrays.copyOf(elementData, size);
v.modCount = 0;
return v;
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
//返回一个按照正确的顺序包含此列表中所有元素的数组
public Object[] toArray() {
return Arrays.copyOf(elementData, size);
}
//返回一个按照正确的顺序包含此列表中所有元素的数组;返回数组的类型就是指定数组的运行时类型。
public <T> T[] toArray(T[] a) {
if (a.length < size)
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
E elementData(int index) {
return (E) elementData[index];
}
//返回此列表中指定位置上的元素
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
//用指定的元素替代此列表中指定位置上的元素
public E set(int index, E element) {
rangeCheck(index);
E oldValue = elementData(index);
elementData[index] = element;
return oldValue;
}
//将指定的元素追加到此列表的尾部
public boolean add(E e) {
ensureCapacityInternal(size + 1);
elementData[size++] = e;
return true;
}
//将指定的元素插入此列表中的指定位置
public void add(int index, E element) {
rangeCheckForAdd(index);
ensureCapacityInternal(size + 1);
System.arraycopy(elementData, index, elementData, index + 1, size - index);
elementData[index] = element;
size++;
}
//移除此列表中指定位置上的元素
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;
return oldValue;
}
//从此列表中移除指定元素的单个实例(如果存在)。
public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++)
if (elementData[index] == null) {
fastRemove(index);
return true;
}
} else {
for (int index = 0; index < size; index++)
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
return false;
}
//移除指定位置的元素
private void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0)
System.arraycopy(elementData, index+1, elementData, index, numMoved);
elementData[--size] = null;
}
//移除此列表中的所有元素
public void clear() {
modCount++;
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
//将指定集合中的所有元素追加到此列表的尾部
public boolean addAll(Collection<? extends E> c) {
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew);
System.arraycopy(a, 0, elementData, size, numNew);
size += numNew;
return numNew != 0;
}
//从指定的位置开始,将指定集合中的所有元素追加到此列表的尾部
public boolean addAll(int index, Collection<? extends E> c) {
rangeCheckForAdd(index);
Object[] a = c.toArray();
int numNew = a.length;
ensureCapacityInternal(size + numNew);
int numMoved = size - index;
if (numMoved > 0)
System.arraycopy(elementData, index, elementData, index + numNew, numMoved);
System.arraycopy(a, 0, elementData, index, numNew);
size += numNew;
return numNew != 0;
}
//移除列表中索引在 fromIndex(包括)和 toIndex(不包括)之间的所有元素
protected void removeRange(int fromIndex, int toIndex) {
modCount++;
int numMoved = size - toIndex;
System.arraycopy(elementData, toIndex, elementData, fromIndex, numMoved);
int newSize = size - (toIndex-fromIndex);
for (int i = newSize; i < size; i++) {
elementData[i] = null;
}
size = newSize;
}
//检查下标范围
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
private void rangeCheckForAdd(int index) {
if (index > size || index < 0)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
//删除列表中包含的指定集合中的元素
public boolean removeAll(Collection<?> c) {
return batchRemove(c, false);
}
//得到列表中包含的指定集合中的元素
public boolean retainAll(Collection<?> c) {
return batchRemove(c, true);
}
private boolean batchRemove(Collection<?> c, boolean complement) {
final Object[] elementData = this.elementData;
int r = 0, w = 0;
boolean modified = false;
try {
for (; r < size; r++)
if (c.contains(elementData[r]) == complement)
elementData[w++] = elementData[r];
} finally {
if (r != size) {
System.arraycopy(elementData, r, elementData, w, size - r);
w += size - r;
}
if (w != size) {
for (int i = w; i < size; i++)
elementData[i] = null;
modCount += size - w;
size = w;
modified = true;
}
}
return modified;
}
//通过JAVA对象流进行序列化和反序列化
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException{
int expectedModCount = modCount;
s.defaultWriteObject();
s.writeInt(size);
for (int i=0; i<size; i++) {
s.writeObject(elementData[i]);
}
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
elementData = EMPTY_ELEMENTDATA;
s.defaultReadObject();
s.readInt();
if (size > 0) {
ensureCapacityInternal(size);
Object[] a = elementData;
for (int i=0; i<size; i++) {
a[i] = s.readObject();
}
}
}
//从指定位置返回listIterator迭代器
public ListIterator<E> listIterator(int index) {
if (index < 0 || index > size)
throw new IndexOutOfBoundsException("Index: "+index);
return new ListItr(index);
}
//返回listIterator迭代器
public ListIterator<E> listIterator() {
return new ListItr(0);
}
//返回Iterator迭代器
public Iterator<E> iterator() {
return new Itr();
}
效率问题:
查询、修改的效率高
增加、删除的效率较低
ArrayList和数组的异同点:
相同点:
ArrayList底层是数组,具有数组的特性。
不同点:
1、大小:数组大小初始化时需固定、ArrayList可以不关注大小(动态扩容)
2、存储数据类型:数组可以存储基本类型和引用类型,ArrayList只可以存储引用类型
集合的处理(并集、交集、差集):
并集(A并B):两集合所有元素的总和,分为可重复并集和无重复并集
交集(A交B): 两个集合中均有的元素
差集(A减B):集合A中除了和集合B公有的元素之外的所有元素
可重复并集:
a.addAll(b);
无重复并集:
a.addAll(b);
b.retainAll(a);
a.removeAll(b);
交集:
a.retainAll(b);
差集:
b.retainAll(a);
a.removeAll(b);