ArrayList底層數據結構是數組
集合存在於Java.util包路徑下
特點:
重複性:數據可以重複
null值:可以有null值存在
有序性:能保證數據的插入有序
常用方法介紹
int size(); 集合中存儲元素的個數
boolean isEmpty(); 判斷當前集合是否爲空,返回值是布爾類型:false(集合不爲空)true(集合爲空)
boolean contains(Object o); 判斷當前集合是否包含該對象
Iterator iterator(); 迭代器,返回iterator實例
Object[] toArray(); 將集合轉化爲數組
T[] toArray(T[] a); 將集合轉化爲數組
boolean add(E e); 添加元素
boolean remove(Object o); 刪除元素
boolean containsAll(Collection<?> c); 判斷入參集合是否屬於當前集合
boolean addAll(Collection<? extends E> c); 對該集合添加子集合形成新的集合
boolean addAll(int index, Collection<? extends E> c); 在指定的位置添加子集合
void clear(); 將集合清除掉
boolean equals(Object o); 判斷是否相等
E get(int index); 通過指定位置來獲取元素
int indexOf(Object o); 判斷當前元素在集合中的位置(從前往後找第一次出現的位置)
int lastIndexOf(Object o); 判斷元素位置(從集合尾部往前查找)
List subList(int fromIndex, int toIndex); 找當前集合的子集(左閉右開,不包含最後一個)
繼承關係
public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable
ArrayList繼承AbstractList,父類中對部分接口進行實現
實現了List接口提供的方法,Serializable說明該類能夠被序列化 ,能夠被克隆、序列化
基本屬性
private static final int DEFAULT_CAPACITY = 10;
默認容量大小
private static final Object[] EMPTY_ELEMENTDATA = {};
默認數組大小
private transient Object[] elementData;
存儲元素的數組
private int size;
集合存儲元素的個數
構造函數
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;
}
//無參構造
public ArrayList(Collection<? extends E> c) {
elementData = c.toArray();
size = elementData.length;
// c.toArray might (incorrectly) not return Object[] (see 6260652)
if (elementData.getClass() != Object[].class)
elementData = Arrays.copyOf(elementData, size, Object[].class);
}
//有參構造,通過集合來創建新的集合
增長方式
按照原數組的1.5倍進行擴容
add()添加元素
首先需要進行擴容考慮,如果要擴容(size+1 > table.lengrh),按照1.5倍進行擴容
將元素插入數組尾部,並對計數size+1
public boolean add(E e) {
ensureCapacityInternal(size + 1); // Increments modCount!!
elementData[size++] = e;
return true;
}
private void ensureCapacityInternal(int minCapacity) {
if (elementData == EMPTY_ELEMENTDATA) {
minCapacity = Math.max(DEFAULT_CAPACITY, minCapacity);
}
ensureExplicitCapacity(minCapacity);
}
private void ensureExplicitCapacity(int minCapacity) {
modCount++;
// overflow-conscious code
if (minCapacity - elementData.length > 0)
grow(minCapacity);
}
private void grow(int minCapacity) {
// overflow-conscious code
int oldCapacity = elementData.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity;
if (newCapacity - MAX_ARRAY_SIZE > 0)
newCapacity = hugeCapacity(minCapacity);
// minCapacity is usually close to size, so this is a win:
elementData = Arrays.copyOf(elementData, newCapacity);
}
remove()刪除元素
先找到元素存儲位置(null和非null的對象判斷相等的不同)
數組移動
注意:相同元素存在的情況下,只需找到從0號開始第一次出現的元素
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; // clear to let GC do its work
}
get()獲取元素
public E get(int index) {
rangeCheck(index);
return elementData(index);
}
private void rangeCheck(int index) {
if (index < 0 || index >= this.size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
E elementData(int index) {
return (E) elementData[index];
}
ArrayList在擴容時,調用了Arrays.copyOf(),在這裏,對Arrays.copyOf()和System.arraycopy()兩種拷貝做簡要對比:
Arrays.copyOf()
參數:原數組,拷貝數組的長度
調用了重載方法,創建了新的數組,調用了System.arraycopy()方法
System.arraycopy()
參數:原數組,原數組拷貝的起始位置,目標數組,
目標數組拷貝的起始位置,拷貝長度
native
從兩種拷貝方式的定義來看:
System.arraycopy()使用時必須有原數組和目標數組,
Arrays.copyOf()使用時只需要有原數組即可。
從兩種拷貝方式的底層實現來看:
System.arraycopy()是用c或c++實現的,
Arrays.copyOf()是在方法中重新創建了一個數組,
並調用System.arraycopy()進行拷貝。
兩種拷貝方式的效率分析:
由於Arrays.copyOf()不但創建了新的數組而且最終還是調用System.arraycopy(),所以System.arraycopy()的效率高於Arrays.copyOf()。