ArrayList 学习笔记
先摆上JDK1.8中ArrayList的类注释;我翻译了一下
/** * 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
总结:
1.ArrayList实现List接口,它的兄弟还有LinkedLIst,Vector,所有经常被问它们的区别也就情有可原了
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异常
。。。后续补充