源码分析——What is ArrayList

Posted 喵喵7781

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了源码分析——What is 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.)


 可变数组实现List接口。实现可选列表,并允许所有元素,包括null。除了实现List接口,这个类提供了操作数组大小的方法用于内部存储列表数据。 (这个类大致相当于 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 iterator,以及listIterator这些方法以常量的形式运行。  add 操作在 分摊的常量时间  中运行,即添加 n个元素需要O(n)时间。所有其他操作都以线性时间运行(粗略地说)。与 LinkedList实现相比,constant factor较低。


  <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时,其容量会自动增加。除了添加元素具有恒定的时间成本这一事实之外,未指定增长策略的详细信息。


  <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.

在使用ensureCapacity 操作添加大量元素之前,应用程序可以增加 ArrayList 实例的容量。这可能会减少增量重新分配的数量。


<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实例,并且至少有一个线程在结构上修改了列表,则必须外部同步。 (结构修改是添加或删除一个或多个元素的任何操作,或显式调整后备数组的大小; 仅设置元素的值不是结构修改。)这通常通过在自然封装列表的某个对象上同步来完成。


  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>
  <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.


 如果不存在此类对象,则应使用@link CollectionssynchronizedList Collections.synchronizedList“包装”该列表。
方法。这最好在创建时完成,以防止意外地不同步访问列表:List list = Collections.synchronizedList(new ArrayList(...)); 
 <a name='fail-fast'>此类的@link iterator()iterator和@link listIterator(int)listIterator方法返回的迭代器<em> fail-fast </ em>: </a>如果在创建迭代器之后的任何时候对列表进行结构修改,除非通过迭代器自己的@link ListIteratorremove()remove或@link ListIteratoradd(Object)add方法,否则迭代器将抛出一个@link ConcurrentModificationException。因此,面对并发修改,迭代器会快速而干净地失败,而不是在未来的未确定时间冒任意,非确定性行为的风险。


  <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>

<p>请注意,迭代器的快速失败行为无法得到保证,因为一般来说,在存在非同步并发修改的情况下,不可能做出任何硬性保证。失败快速的迭代器会尽最大努力抛出@code ConcurrentModificationException。因此,为了正确而编写一个依赖于此异常的程序是错误的
:迭代器的fail-fast行为应仅用于检测错误。

 

参考资料:

JDK1.8源码

fail-fast机制
 

 

以上是关于源码分析——What is ArrayList的主要内容,如果未能解决你的问题,请参考以下文章

源码分析——What is ConcurrentHashMap

源码分析——What is ConcurrentHashMap

源码分析——What is LinkedHashMap

源码分析——What is LinkedHashMap

源码分析——What is LinkedList

源码分析——What is LinkedList