ArrayList 學習筆記

先擺上JDK1.8中ArrayList的類註釋;我翻譯了一下html

/**
 * Resizable-array implementation of the <tt>List</tt> interface.  Implements
 * all optional list operations, and permits all elements, including
 * <tt>null</tt>.  In addition to implementing the <tt>List</tt> interface,
 * this class provides methods to manipulate the size of the array that is
 * used internally to store the list.  (This class is roughly equivalent to
 * <tt>Vector</tt>, except that it is unsynchronized.)
 * ArrayList實現了List接口,它容許存儲全部類型的元素,包括null,它的特色是可變長的List,能夠動態修改其容量
 * 由於ArrayList內部實現是數組(Vector),它是線程不一樣步的.
 *
 * <p>The <tt>size</tt>, <tt>isEmpty</tt>, <tt>get</tt>, <tt>set</tt>,
 * <tt>iterator</tt>, and <tt>listIterator</tt> operations run in constant
 * time.  The <tt>add</tt> operation runs in <i>amortized constant time</i>,
 * that is, adding n elements requires O(n) time.  All of the other operations
 * run in linear time (roughly speaking).  The constant factor is low compared
 * to that for the <tt>LinkedList</tt> implementation.
 * size(),isEmpty(),get(),set(),迭代ArrayList,這些對於它的操做時間複雜是O(1),
 * add(E)操做,直接在集合末端加入元素,時間複雜度也是O(1),
 * 剩下的操做,好比add(int index,E)指定位置添加 remove()移除,時間複雜度都是線性階的,即O(n)。
 * 這操做性能是低於LinkedList的
 * 由於ArrayList是基於數組實現的,因此保留了數組的特徵.插入刪除元素後集合內的其餘元素都要發生位置變化
 * 這時候調用System.arraycopy這是很費性能的.
 *
 * <p>Each <tt>ArrayList</tt> instance has a <i>capacity</i>.  The capacity is
 * the size of the array used to store the elements in the list.  It is always
 * at least as large as the list size.  As elements are added to an ArrayList,
 * its capacity grows automatically.  The details of the growth policy are not
 * specified beyond the fact that adding an element has constant amortized
 * time cost.
 * 每一個ArrayList實例都有一個容量,用來表示在集合中元素的個數.這個容量通常都比實際存儲的
 * 元素個數要大一點.當一個元素被添加到ArrayList中時,ArrayList的容量就自動增加了.在ArrayList的
 * 尾部添加元素的時間複雜度是O(1)
 *
 * <p>An application can increase the capacity of an <tt>ArrayList</tt> instance
 * before adding a large number of elements using the <tt>ensureCapacity</tt>
 * operation.  This may reduce the amount of incremental reallocation.
 * 當程序在向一個ArrayList中添加大量元素時,能夠調用ensureCapacity方法.
 * 它會下降當集合容量增量分配的次數(初始化時能提高性能)
 *
 * <p><strong>Note that this implementation is not synchronized.</strong>
 * If multiple threads access an <tt>ArrayList</tt> instance concurrently,
 * and at least one of the threads modifies the list structurally, it
 * <i>must</i> be synchronized externally.  (A structural modification is
 * any operation that adds or deletes one or more elements, or explicitly
 * resizes the backing array; merely setting the value of an element is not
 * a structural modification.)  This is typically accomplished by
 * synchronizing on some object that naturally encapsulates the list.
 * 注意ArrayList對List的實現是線程不一樣步的.若是多線程併發訪問一個ArrayList實例,而且至少一個線程在修改ArrayList結構,
 * 這個操做必須在上層進行線程同步.任何一個修改結構的操做,包括添加,刪除一個或多個元素,調整集合容量,除非是設置一個元素的值而不是結構性的修改,
 * 這些操做一般都是經過封裝一個對象而後進行操做的(封裝成對象,對對象的訪問操做,保證線程同步,防止異常發生)
 *
 * If no such object exists, the list should be "wrapped" using the
 * {@link Collections#synchronizedList Collections.synchronizedList}
 * method.  This is best done at creation time, to prevent accidental
 * unsynchronized access to the list:<pre>
 *   List list = Collections.synchronizedList(new ArrayList(...));</pre>
 *  含義同上,由於線程不一樣步問題,爲避免異常發生,應當在操做集合的上層應用進行線程同步處理.
 *  可使用 List list = Collections.synchronizedList(new ArrayList(...));這個進行實現,這是線程同步的
 *
 * <p><a name="fail-fast">
 * The iterators returned by this class's {@link #iterator() iterator} and
 * {@link #listIterator(int) listIterator} methods are <em>fail-fast</em>:</a>
 * if the list is structurally modified at any time after the iterator is
 * created, in any way except through the iterator's own
 * {@link ListIterator#remove() remove} or
 * {@link ListIterator#add(Object) add} methods, the iterator will throw a
 * {@link ConcurrentModificationException}.  Thus, in the face of
 * concurrent modification, the iterator fails quickly and cleanly, rather
 * than risking arbitrary, non-deterministic behavior at an undetermined
 * time in the future.
 *  ArrayList採用fail-fast機制.
 *
 * <p>Note that the fail-fast behavior of an iterator cannot be guaranteed
 * as it is, generally speaking, impossible to make any hard guarantees in the
 * presence of unsynchronized concurrent modification.  Fail-fast iterators
 * throw {@code ConcurrentModificationException} on a best-effort basis.
 * Therefore, it would be wrong to write a program that depended on this
 * exception for its correctness:  <i>the fail-fast behavior of iterators
 * should be used only to detect bugs.</i>
 * fail-fast機制,是一種錯誤檢測機制。它只能被用來檢測錯誤,由於JDK並不保證fail-fast機制必定會發生.
 *
 * <p>This class is a member of the
 * <a href="{@docRoot}/../technotes/guides/collections/index.html">
 * Java Collections Framework</a>.
 *
 * @author  Josh Bloch
 * @author  Neal Gafter
 * @see     Collection
 * @see     List
 * @see     LinkedList
 * @see     Vector
 * @since   1.2
 */
public class ArrayList<E> extends AbstractList<E>
implements List<E>, RandomAccess, Cloneable, java.io.Serializable

 

總結:java

  1.ArrayList實現List接口,它的兄弟還有LinkedLIst,Vector,全部常常被問它們的區別也就情有可原了api

  2.ArrayList內部是基於數組實現的,也叫動態數組.全部它插入刪除效率低,隨即訪問效率高數組

      3.它是線程不安全的,參考「ArrayList 線程安全」                      安全

      4.它是由容量上限的,api中寫的是Integer.MAX_VALUE-8 ,也就是2^31 -8 多線程

  5.add(E)尾部插入,get(E),get(index) 時間複雜度都是O(1);add(index,E),remove(E),remove(index,E)時間複雜度是O(n),性能差併發

  6.ArrayList程不一樣步,採用fail-fast機制。多線程下,在迭代器中,若是有線程修改了ArrayList結構,會拋出 Java.util.ConcurrentModificationException異常app

  

     。。。後續補充dom

相關文章
相關標籤/搜索