Java ArrayList 详解 - TongtongLan/Java GitHub Wiki

一个可动态调整数组大小的list接口实现。实现了所有list接口中的方法,并提供一些可自动扩展数组大小的方法。

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.

请注意,此实现不同步。如果多个线程同时访问ArrayList实例,并且至少有一个线程在结构上修改了列表,则它必须在外部同步。(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 Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list:

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.

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.

  • 具备三个构造方法
  1. public ArrayList()

Constructs an empty list with an initial capacity of ten.

  1. public ArrayList(Collection<? extends E> c)

Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

调用 Collections.toArray() 方法将Collection对象转化为数组,调用Array.copyOf() 方法将数组保存为ArrayList实例。

  1. public ArrayList(int initialCapacity)

Constructs an empty list with the specified initial capacity.

this.elementData = new Object[initialCapacity];

  • 修改数组容量。如有必要,增加此ArrayList实例的容量,以确保它至少能容纳由最小容量参数指定的元素数量。

可通过调用 public void ensureCapacity(int minCapacity) 修改数组容量

public void ensureCapacity(int minCapacity) {
    int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
        // any size if not default element table
        ? 0
        // larger than default for default empty table. It's already
        // supposed to be at default size.
        // DEFAULT_CAPACITY:Default initial capacity.
        : DEFAULT_CAPACITY;

    if (minCapacity > minExpand) {
        ensureExplicitCapacity(minCapacity);
    }
}
  • 数组排序

sort(Comparator<? super E> c)

Sorts this list according to the order induced by the specified Comparator.

  • 将实例对象的容量设置为当前list的大小

trimToSize()

Trims the capacity of this ArrayList instance to be the list's current size.应用程序可以使用此操作来最小化ArrayList实例的存储。

延伸

How to sort ArrayList in java - List Sorting - Ascending Descending Order

About Vector

Vector是同步的。如果不需要线程安全的实现,建议使用ArrayList来代替Vector。

⚠️ **GitHub.com Fallback** ⚠️