1. 程式人生 > >Java ArrayList集合常用方法

Java ArrayList集合常用方法

建立物件:與其他普通的引用資料型別建立方式完全相同,但要指定容器中儲存的資料型別:

ArrayList<要儲存元素的資料型別> 變數名 = new ArrayList<要儲存元素的資料型別>();

集合中儲存的元素,只能為<>括號中指定的資料型別元素;
“<要儲存元素的資料型別>”中的資料型別必須是引用資料型別,不能是基本資料型別;
下面給出8種基本資料型別所對應的引用資料型別表示形式:

基本資料型別 對應的引用資料型別表示形式
byte Byte
short Short
Int Integer
long Long
float Float
double Double
char Character
boolean Boolean

附上java基本型別
這裡寫圖片描述

常用方法
方法宣告 功能描述
boolean add(Object obj) 將指定元素obj追加到集合的末尾
Object get(int index) 返回集合中指定位置上的元素
int size() 返回集合中的元素個數
boolean add(int index, Object obj) 將指定元素obj插入到集合中指定的位置
Object remove(int index) 從集合中刪除指定index處的元素,返回該元素
void clear() 清空集合中所有元素
Object set(int index, Object obj) 用指定元素obj替代集合中指定位置上的元素

ArrayList原始碼解析

package java.util;

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{
    // 序列版本號
    private static final long serialVersionUID = 8683452581122892189L;

    // 預設容量大小
    private static final
int DEFAULT_CAPACITY = 10; // 空陣列 private static final Object[] EMPTY_ELEMENTDATA = {}; // 用於儲存ArrayList中資料的陣列 private transient Object[] elementData; // ArrayList中所包含元素的個數 private int size; // 帶初始容量引數的建構函式 public ArrayList(int initialCapacity) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; } // 預設建構函式,其預設初始容量為10 public ArrayList() { super(); this.elementData = EMPTY_ELEMENTDATA; } // 帶Collection引數的建構函式 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); } // 將此 ArrayList 例項的容量調整為列表的當前大小(實際元素個數) public void trimToSize() { modCount++; if (size < elementData.length) { elementData = Arrays.copyOf(elementData, size); } } // 如有必要,增加此 ArrayList 例項的容量,以確保它至少能夠容納最小容量引數所 // 指定的元素數 public void ensureCapacity(int minCapacity) { int minExpand = (elementData != EMPTY_ELEMENTDATA) // any size if real element table ? 0 // larger than default for empty table. It's already supposed to be // at default size. : DEFAULT_CAPACITY; if (minCapacity > minExpand) { ensureExplicitCapacity(minCapacity); } } 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 static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; 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); } private static int hugeCapacity(int minCapacity) { if (minCapacity < 0) // overflow throw new OutOfMemoryError(); return (minCapacity > MAX_ARRAY_SIZE) ? Integer.MAX_VALUE : MAX_ARRAY_SIZE; } // 返回ArrayList中的元素個數 public int size() { return size; } // 判斷ArrayList是否為空 public boolean isEmpty() { return size == 0; } // 判斷ArrayList是否包含Object(o) public boolean contains(Object o) { return indexOf(o) >= 0; } // 返回ArrayList中首次出現的指定元素的索引,或如果此列表不包含元素,則返回 -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; } // 返回ArrayList中最後一次出現的指定元素的索引,或如果此列表不包含索引,則返回 -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 { @SuppressWarnings("unchecked") ArrayList<E> v = (ArrayList<E>) super.clone(); // 將當前ArrayList的全部元素拷貝到v中 v.elementData = Arrays.copyOf(elementData, size); v.modCount = 0; return v; } catch (CloneNotSupportedException e) { // this shouldn't happen, since we are Cloneable throw new InternalError(); } } // 按適當順序(從第一個到最後一個元素)返回包含此列表中所有元素的陣列 public Object[] toArray() { return Arrays.copyOf(elementData, size); } // 返回ArrayList的模板陣列。所謂模板陣列,即可以將T設為任意的資料型別 @SuppressWarnings("unchecked") public <T> T[] toArray(T[] a) { if (a.length < size) // Make a new array of a's runtime type, but my contents: return (T[]) Arrays.copyOf(elementData, size, a.getClass()); System.arraycopy(elementData, 0, a, 0, size); if (a.length > size) a[size] = null; return a; } // 位置訪問操作 @SuppressWarnings("unchecked") E elementData(int index) { return (E) elementData[index]; } // 返回ArrayList中指定位置上的元素 public E get(int index) { rangeCheck(index); return elementData(index); } // 用指定的元素替代ArrayList中指定位置上的元素,並返回替代前的元素 public E set(int index, E element) { rangeCheck(index); E oldValue = elementData(index); elementData[index] = element; return oldValue; } // 將指定的元素新增到ArrayList的尾部 public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; } // 將指定的元素插入ArrayList中的指定位置 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++; } // 移除ArrayList中指定位置上的元素,並返回該位置上的元素 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; } // 移除ArrayList中首次出現的指定元素(如果存在則移除並返回true,否則返回false) 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 } // 移除ArrayList中的所有元素 public void clear() { modCount++; // clear to let GC do its work for (int i = 0; i < size; i++) elementData[i] = null; size = 0; } // 按照指定 collection 的迭代器所返回的元素順序, // 將該 collection 中的所有元素新增到ArrayList的尾部 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; } // 從指定的位置開始,將指定 collection 中的所有元素插入到ArrayList中 public boolean addAll(int index, Collection<? extends E> c) { rangeCheckForAdd(index); Object[] a = c.toArray(); int numNew = a.length; ensureCapacityInternal(size + numNew); // Increments modCount 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); // clear to let GC do its work 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)); } // 私有方法,用於add和addAll private void rangeCheckForAdd(int index) { if (index > size || index < 0) throw new IndexOutOfBoundsException(outOfBoundsMsg(index)); } private String outOfBoundsMsg(int index) { return "Index: "+index+", Size: "+size; } // 移除ArrayList中Collection所包含的所有元素 public boolean removeAll(Collection<?> c) { return batchRemove(c, false); } // 保留所有ArrayList和Collection共有的元素 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 { // Preserve behavioral compatibility with AbstractCollection, // even if c.contains() throws. if (r != size) { System.arraycopy(elementData, r, elementData, w, size - r); w += size - r; } if (w != size) { // clear to let GC do its work for (int i = w; i < size; i++) elementData[i] = null; modCount += size - w; size = w; modified = true; } } return modified; } // java.io.Serializable的寫入函式 // 將ArrayList的“容量,所有的元素值”都寫入到輸出流中 private void writeObject(java.io.ObjectOutputStream s) throws java.io.IOException{ // Write out element count, and any hidden stuff int expectedModCount = modCount; s.defaultWriteObject(); // Write out size as capacity for behavioural compatibility with clone() s.writeInt(size); // Write out all elements in the proper order. for (int i=0; i<size; i++) { s.writeObject(elementData[i]); } if (modCount != expectedModCount) { throw new ConcurrentModificationException(); } } // java.io.Serializable的讀取函式:根據寫入方式讀出 // 先將ArrayList的“容量”讀出,然後將“所有的元素值”讀出 private void readObject(java.io.ObjectInputStream s) throws java.io.IOException, ClassNotFoundException { elementData = EMPTY_ELEMENTDATA; // Read in size, and any hidden stuff s.defaultReadObject(); // Read in capacity s.readInt(); // ignored if (size > 0) { // be like clone(), allocate array based upon size not capacity ensureCapacityInternal(size); Object[] a = elementData; // Read in all elements in the proper order. 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(); } // 返回一個指定範圍的子List列表 public List<E> subList(int fromIndex, int toIndex) { subListRangeCheck(fromIndex, toIndex, size); return new SubList(this, 0, fromIndex, toIndex); } }