JDK8 ArrayList源码分析

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!

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的大小一样大。当元素被添加到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 the Collections.synchronizedList method. This is best done at creation time, to prevent accidental unsynchronized access to the list: 注意:这个实现是非线程安全的。如果多个线程同时访问一个ArrayList实例,并且至少一个线程在结构上修改这个list,它必须在外部被同步。(结构性的修改是指任何添加或删除一个或多个元素、调整内部数组大小;仅仅只是设置一个元素的值不是一个结构性的修改。)这通常是通过同步某一对象自然地封装这个list来实现的。如果没有这样的对象存在,这个list应当被用Collections.synchronizedList方法来包装。为了预防意外的非同步访问这个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. 这个类的iterator方法和listIterator方法返回的迭代器是fail-fast的:如果在迭代器被创建后这个list被在结构上改变了,除非是通过迭代器自身的remove或者add方法,这个迭代器将会抛出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 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行为应当只是被用来定位问题。

2. elementData不会被序列化

=====================

transient Object[] elementData;

复制代码

从变量声明情况来看,如果序列化ArrayList实例,elementData存储的数据是不会保存下来的,实际开发中后端暴露的api返回的对象中常常包含ArrayList,这个数据是怎么传递的?具体情况待验证。

3. 内部数组elementData扩容

=====================

private static final int DEFAULT_CAPACITY = 10;

复制代码

默认容量是10。

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

}

总结

本文从基础到高级再到实战,由浅入深,把MySQL讲的清清楚楚,明明白白,这应该是我目前为止看到过最好的有关MySQL的学习笔记了,我相信如果你把这份笔记认真看完后,无论是工作中碰到的问题还是被面试官问到的问题都能迎刃而解!

MySQL50道高频面试题整理:

《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!
一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》点击传送门,即可获取!**

  • 8
    点赞
  • 30
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值