ArrayList(JDK8)详解①——序言翻译

序:

也许你会问,ArrayList直接看Java API的说明文档直接调用不就够用了吗,为啥还要深入了解?

其实不然,虽然Java帮我们做了很多事,但是作为一个上进的Java程序员,想要变得更强,阅读JDK源码还是很重要的,这些包基本上都是Java界著名的大牛写下的,深入源码进行分析也是一种学习和提升的途径。


下面先看ArrayList介绍

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

List接口的可调整大小的数组实现。实现所有可选的列表操作,并且允许所有元素,包括null。除了实现List接口,该类还提供了一些方法来调整作用于内部存储列表的数组的大小。(这个类大致相当于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操作的时间是常数时间,add操作在均摊时间为常数时间,即添加n个元素需要O(n)时间复杂度。其它所有的操作都在线性时间内运行(粗略地说)。与LinkedList实现相比,ArrayList常数因子较低

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.

扫描二维码关注公众号,回复: 5755428 查看本文章

每个ArrayList实例都有一个容量。容量用来存储列表中元素的数组的大小。其容量始终至少和列表大小一样大。随着元素被加到ArrayList中,其容量也会自动增长。除了添加元素具有恒定的均摊时间成本这一事实之外,并未指定增长策略的详细信息。

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. If no such object exists, the list should be "wrapped" using theCollections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:

   List list = Collections.synchronizedList(new ArrayList(...));

注意,这个实现是不同步的。如果多个线程同时访问一个ArrayList实例,并且至少有一个线程在结构上修改表,则必须在外部同步该列表。结构修改是添加或删除一个或多个元素、或显示调整备份数组的任何操作,仅设置元素的值而不是结构修改。)这通常是通过同步一些自然封装到列表的类来完成的。如果没有这样的对象存在,应该用Collection.synchronize方法“包装”这个列表:

(示例代码)List list = Collections.synchronizedList(new 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 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.

此类的iterator和listIterator方法返回的iterator是快速失败的:如果在创建iterator之后对list进行结构修改,除了iterator本身的remove和add方法外iterator将会抛出ConcurrentModificationException。因此在并发修改的情况下,iterator会快速而清晰地失败,而不是在未来不确定的时间内冒任意、不确定的风险。

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.

注意,照目前的情况看,迭代器的快速失败行为无法得到保证,一般来说,在不同步的并发修改的场合,不可能做出任何硬保证。快速失败iterator会尽最大的努力的基础上抛出ConcurrentModificationException。因此,编写一个依赖于这个异常的程序来保证其正确性的行为是错误的:iterator的快速失败行为应该只用于检测错误。

This class is a member of the Java Collections Framework.

这个类是Java集合(Java Collection)框架的成员。


概括:ArrayList实现了List接口,底层是数组实现,存放的数据是有序的,并且允许数据是NULL值。ArrayList容量始终至少和列表大小一样大,并且添加元素时会自动扩容。ArrayList的查找操作,时间复杂度为O(1),故此查找比LinkedList速度快很多。ArrayList的实现不是同步的,所以其不支持并发,多线程情况下需使用List list = Collections.synchronizedList(new ArrayList(...));

猜你喜欢

转载自blog.csdn.net/qq_36709271/article/details/88592413
今日推荐