带你一起看源码系列之--ArrayList

ArrayList

ArrayList概览(英文注解详解)

public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable

  • Resizable-array implementation of the List interface. 实现List接口的可变数组。(ArrayList就是传说中的动态数组
  • Implements all optional list operations, and permits all elements, including null. 实现所有可选的列表的操作,并且允许添加所有元素包括null
  • In addition to implementing the List interface, this class provides methods to manipulate the size of the array that is used internally to store the list. 除了实现List接口之外,此类还提供了一些方法来操作内部用于存储列表的数组的大小。
  • (This class is roughly equivalent to Vector, except that it is unsynchronized.) 此类大致相当于Vector类,除了此类是不同步的(即线程不安全)。
  • The size, isEmpty, get, set,iterator, and listIterator operations run in constant time. The add operation runs in amortized constant time, that is, adding n elements requires O(n) time. size,isEmpty,get,set,iterato,listIterator方法在恒定的时间内运行。add方法在分摊的恒定时间内运行,添加n个元素需要O(n)时间。

  • All of the other operations run in linear time (roughly speaking). The constant factor is low compared to that for the LinkedList implementation. 所有其他方法都以线性时间运行(粗略地说)。常量因子与LinkedList实现的相比较低。
  • Each ArrayList instance has a capacity. 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实例都有capacity(容量)。capacity (容量)是用于列表中存储元素的数组的大小。它一直至少是和list的大小一样大。当元素加入打一个ArrayList中去的时候,它的capacity会自动增长。除了添加一个元素具有恒定的分摊时间这一事实之外,还有没有制定增长策略的细节。

  • An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation. 在添加大量的元素之前使用ensureCapacity方法,应用程序可以扩大一个ArrayList的实例的容量。这可能会减少再分配增长的数量。

  • Note that this implementation is not synchronized. If multiple threads access an ArrayList instance concurrently, and at least one of the threads modifies the list structurally, it must 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:List list = Collections.synchronizedList(new ArrayList(...)); 如果没有这样的对象存在,这个集合应该使用Collections.synchronizedList 方法来封装。这最好在创建的时刻完成,以防止突然地非同步的对集合的访问,List list = Collections.synchronizedList(new ArrayList(...));
  • The iterators returned by this class's {@link #iterator() iterator} and {@link #listIterator(int) listIterator} methods are fail-fast: 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. fail-fast 通过这个类的iterator()listIterator(int)返回的迭代器是fail-fast的。如果列表在迭代器创建之后在任意时刻在结构上被修改,不管如何除了通过迭代器自己的remove或者add方法,迭代器会抛出一个ConcurrentModificationException异常。因此,在面对ConcurrentModificationException异常的时候,迭代器快速地、干净地结束,而不是冒着在未来一个不确定的时间任意的、不确定的行为的风险。
  • 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: the fail-fast behavior of iterators should be used only to detect bugs. 注意,迭代器的fail-fast行为不能够保证,因为通常来说,在存在不同步的并发修改场景下,不可能做出任何硬保证。Fail-fast的迭代器为了最大的效益,抛出了ConcurrentModificationException异常。一次,依靠这个异常来保证正确性来写程序是错误的:迭代器的fail-fast行为只应该用来侦测bugs。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java中ArrayList类的部分代码(摘自OpenJDK 8): ``` public class ArrayList<E> extends AbstractList<E> implements List<E>, RandomAccess, Cloneable, java.io.Serializable { //默认初始化容量 private static final int DEFAULT_CAPACITY = 10; //空数组实例 private static final Object[] EMPTY_ELEMENTDATA = {}; //缺省空数组实例,用于默认构造函数创建空列表 private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {}; //元素数组 transient Object[] elementData; // non-private to simplify nested class access //构造函数 public ArrayList(int initialCapacity) { if (initialCapacity > 0) { this.elementData = new Object[initialCapacity]; } else if (initialCapacity == 0) { this.elementData = EMPTY_ELEMENTDATA; } else { throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); } } public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; } private void ensureCapacityInternal(int minCapacity) { if (elementData == DEFAULTCAPACITY_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); } //其他方法,如get、set、size等省略 } ``` 上面的代码展示了ArrayList的实现方式。ArrayList使用一个数组来存储元素,当元素数量达到数组容量时,会自动扩容。默认情况下,数组容量为10,但是在构造函数中可以指定初始化容量。如果元素数量为0,则使用一个空数组。ArrayList还实现了List和RandomAccess接口,以及其他一些方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值