ArrayList API文档翻译

闲话不多说直接上文章,有个别翻译不到位的地方还请见谅。
Resizable-array implementation of the List interface. Implements
all optional list operations, and permits all elements, including
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. (This class is roughly equivalent to
Vector, except that it is unsynchronized.)

ArrayList 是实现List接口大小可变的数组,实现了所有可选的列表操作,并且允许元素为null; ArrayList 除了实现List接口外,还提供用于改变数组内部大小的方法。(AarrayList 除了不是同步的外与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. All of the other operations
run in linear time (roughly speaking). The constant factor is low compared
to that for the LinkedList implementation.

Size,isEmpty,get,set ,iterator,listIterator操作的需要的时间是常量(也就是说这些操作的运行时间与元素个数无关,操作的是时间是O(1)),而add方法则是恒定摊销时间(即与元素个数相关,添加n元素时 需要O(n)),粗略的说,ArrayList所有其他的操作都能在线性时间内完成,(也就是说这些操作与元素的个成线性关系,操作的时间复杂度o(n))。 注意这里的其他指 除了上述提到的方法外;

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的实例都有个容量,这个容量就是用来存储元素的数组大小,它至少要与list的大小一样(这个大小就是指list实际存放元素的个数),当添加一个元素时,这个容量会自动增加,除了要求添加元素的恒定摊销外,对于具体的细节并没有任何要求。

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实例的容量,这样可以因扩容而减少再分配的次数;
Q:为什么会出现为ArrayList重新分配内存的时候?
A:因为ArrayList在添加元素后,如果size+1>capacity,则会重新分配内存。

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, itmust 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.
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(…));<

注意ArrayList是非线性安全的,如果多线程同时访问ArrayList实例,而且至少有一个线程进行了结构性修改,必须在外部进行加锁操作以达到线程安全。任何增加,删除一个元素或者明确修改数组大小都是结构性的修改操作,但仅仅修改某个元素的值并不是结构性修改。这一操作通过封装了同步的实例完成,如果没有 创建方式如下:
List list = Collections.synchronizedList(new ArrayList(…))
这个操作最好在创建的时候完成,以避免意外的不同步的线程访问ArrayList发生。
The iterators returned by this class’s iterator and 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 remove or add methods,the iterator will throw a {{@linkConcurrentModificationException}. 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 通过 iterator()和 listIterator()方法返回的迭代器都会很快的抛出异常,如果再迭代器创建后,除了使用迭代器的remove,add方法外,其他任何对ArrayList进行了结构性修改都会抛出这个异常。因此,在面对并发修改时,迭代器会很直接的完全失效,而不是选择冒着在以后不确定时间发生不确定的行为危险。

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

还需注意的是,一般来说这种快速失效的机制在不同步的并发修改时并不能得到保证。iterators的Fail-fast会尽最大的努力抛出ConcurrentModificationException异常,Fail-fast只用来检查bug,而不能用来作为保证一个程序正确性的依靠。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值