ArrayList
一. 基本描述
ArrayList是一个容量可动态增长的“数组”。 有下面ArrayList实现的源码可以看到,它继承了AbstractList抽象类,实现了List、RandomAccess、Cloneable、java.io.Serializable四个接口
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable{}
二. 源码分析
1.1. 构造方法
-
ArrayList()
// transient Object[] elementData; // private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {} public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; }
-
ArrayList(int initialCapacity)
看下面的源码,如果初始容量大于0,则正常传入一个数组;等于0,则空;小于0,则返回异常。
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); } }
-
ArrayList(Collection<? extends E> c)
传入一个列表作为初始化参数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; } }
1.2. 增加操作
-
add(E e) 添加一个元素到列表的末尾, 先长度加1,然后元素加入末尾。返回值为boolean
public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; }
-
add(int index, E element) 在指定的index位置加入元素,返回值为boolean
public void add(int index, E element) { rangeCheckForAdd(index); ensureCapacityInternal(size + 1); // Increments modCount!! System.arraycopy(elementData, index, elementData, index + 1, size - index); elementData[index] = element; size++; }
-
addAll(Collection<? extends E> c) 添加一个指定的集合到元素的末尾,返回值为boolean
public boolean addAll(Collection<? extends E> c) { Object[] a = c.toArray(); int numNew = a.length; ensureCapacityInternal(size + numNew); // Increments modCount System.arraycopy(a, 0, elementData, size, numNew); size += numNew; return numNew != 0; }
1.3. 删除操作
-
remove(Object o) 删除列表中第一个出现的o元素。返回值boolean
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; }
-
remove(int index) 删除指定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; }
-
removeAll(Collection<?> c) 从arraylist中删除c中包含的所有元素,返回值为boolean
public boolean removeAll(Collection<?> c) { Objects.requireNonNull(c); return batchRemove(c, false); }
-
removeRange(int fromIndex, int toIndex) 删除从fromIndex到toIndex位置出的所有元素
protected void removeRange(int fromIndex, int toIndex) { modCount++; int numMoved = size - toIndex; System.arraycopy(elementData, toIndex, elementData, fromIndex, numMoved); // clear to let GC do its work int newSize = size - (toIndex-fromIndex); for (int i = newSize; i < size; i++) { elementData[i] = null; } size = newSize; }
-
clear() 删除所有元素
public void clear() { modCount++; // clear to let GC do its work for (int i = 0; i < size; i++) elementData[i] = null; size = 0; }
1.4. 更改操作
-
set(int index, E element) 用element替换index位置上的元素
public E set(int index, E element) { rangeCheck(index); E oldValue = elementData(index); elementData[index] = element; return oldValue; }
-
sort(Comparator<? super E> c) 按照指定的规则进行排序 [排序再更新]
public void sort(Comparator<? super E> c) { final int expectedModCount = modCount; Arrays.sort((E[]) elementData, 0, size, c); if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } modCount++; }
-
retainAll(Collection<?> c) 保留列表中出现过的元素,返回boolean
public boolean retainAll(Collection<?> c) {
Objects.requireNonNull(c);
return batchRemove(c, true);
}
1.5. 取数操作
-
isEmpty() 判断列表是否为空,如果为空返回true,否则false
public boolean isEmpty() { return size == 0; }
-
iterator() 返回列表迭代器
public Iterator<E> iterator() { return new Itr(); }
-
size() 返回元素个数,int类型
public int size() { return size; }
-
contains(Object o) 如果队列中有o元素,则返回true,否则false
public boolean contains(Object o) { return indexOf(o) >= 0; }
-
get(int index) 返回index位置处的元素
public E get(int index) { rangeCheck(index); return elementData(index); }
-
indexOf(Object o) 返回元素o第一次出现所在的索引位置信息,如果不存在,返回-1
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; }
-
lastIndexOf(Object o) 返回元素o最后一次出现所在的索引位置信息,如果不存在,返回-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; }
-
subList(int fromIndex, int toIndex) 返回从fromIndex 到 toIndex的元素,返回值List
public List<E> subList(int fromIndex, int toIndex) { subListRangeCheck(fromIndex, toIndex, size); return new SubList(this, 0, fromIndex, toIndex); }
-
toArray() 返回一个包含arrayList中所有元素的数组,返回值为
Object[]
并不改变原先的arrayListpublic Object[] toArray() { return Arrays.copyOf(elementData, size); }