Skip to content

Commit 8be55a1

Browse files
committed
[A] list: ArrayList
1 parent ad93415 commit 8be55a1

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@
4848
│ ├── <a href="./java/source-code/">source-code</a><br>
4949
│ │ └── <a href="./java/source-code/jdk/">jdk</a><br>
5050
│ │ &nbsp;&nbsp;&nbsp; ├── <a href="./java/source-code/jdk/collection/">collection</a><br>
51-
│ │ &nbsp;&nbsp;&nbsp; │ └── <a href="./java/source-code/jdk/collection/collection.md">collection.md</a><br>
51+
│ │ &nbsp;&nbsp;&nbsp; │ ├── <a href="./java/source-code/jdk/collection/collection.md">collection.md</a><br>
52+
│ │ &nbsp;&nbsp;&nbsp; │ └── <a href="./java/source-code/jdk/collection/list-arraylist.md">list-arraylist.md</a><br>
5253
│ │ &nbsp;&nbsp;&nbsp; └── <a href="./java/source-code/jdk/lang/">lang</a><br>
5354
│ │ &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; └── <a href="./java/source-code/jdk/lang/integer.md">integer.md</a><br>
5455
│ ├── <a href="./java/test/">test</a><br>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# ArrayList
2+
3+
## 最大数组容量
4+
5+
> 有两种限制。
6+
>
7+
> 一是规范隐含的限制。
8+
>
9+
> Java数组的length必须是非负的int,
10+
>
11+
> 所以它的理论最大值就是java.lang.Integer.MAX_VALUE = 2^31-1 = 2147483647。
12+
>
13+
> 二是具体的实现带来的限制。
14+
>
15+
> 这会使得实际的JVM不一定能支持上面说的理论上的最大length。例如说如果有JVM使用uint32_t来记录对象大小的话,那可以允许的最大的数组长度(按元素的个数计算)就会是:
16+
>
17+
> (uint32_t的最大值 - 数组对象的对象头大小) / 数组元素大小
18+
>
19+
> 于是对于元素类型不同的数组,实际能创建的数组的最大length也会不同。JVM实现里可以有许多类似的例子会影响实际能创建的数组大小。
20+
21+
作者:RednaxelaFX
22+
链接:https://www.zhihu.com/question/31809233/answer/53370790
23+
来源:知乎
24+
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
25+
26+
> The **count** must be of type int.
27+
>
28+
> if count is less than zero, the anewarray instruction throws a NegativeArraySizeException.
29+
>
30+
> [anewarray - Java Virtual Machine Specification][anewarray]
31+
32+
## 数组容量自增
33+
34+
```java
35+
/**
36+
* The maximum size of array to allocate.
37+
* Some VMs reserve some header words in an array.
38+
* Attempts to allocate larger arrays may result in
39+
* OutOfMemoryError: Requested array size exceeds VM limit
40+
*/
41+
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
42+
43+
private void grow(int minCapacity) {
44+
// overflow-conscious code
45+
int oldCapacity = elementData.length;
46+
// 正常一次扩容为原容量的 3/2
47+
int newCapacity = oldCapacity + (oldCapacity >> 1);
48+
if (newCapacity - minCapacity < 0)
49+
// 新容量溢出,或者新容量依旧没有达到 minCapacity
50+
newCapacity = minCapacity;
51+
if (newCapacity - MAX_ARRAY_SIZE > 0)
52+
newCapacity = hugeCapacity(minCapacity);
53+
// minCapacity is usually close to size, so this is a win:
54+
elementData = Arrays.copyOf(elementData, newCapacity);
55+
}
56+
57+
private static int hugeCapacity(int minCapacity) {
58+
if (minCapacity < 0) // overflow
59+
throw new OutOfMemoryError();
60+
return (minCapacity > MAX_ARRAY_SIZE) ?
61+
Integer.MAX_VALUE :
62+
MAX_ARRAY_SIZE;
63+
}
64+
```
65+
66+
## 开源组件拓展
67+
68+
### `UidGenerator` 环形缓存
69+
70+
![CachedUidGenerator - 双RingBuffer](http://image.rainstorm.vip/blog/ringbuffer.png)
71+
72+
`@see` [UidGenerator][UidGenerator]
73+
74+
### `Disruptor` 高性能队列
75+
76+
// todo
77+
78+
## 参考
79+
80+
- [知乎: Java 数组有最大长度吗? - 作者:RednaxelaFX](https://www.zhihu.com/question/31809233/answer/53370790)
81+
- [anewarray - Java Virtual Machine Specification][anewarray]
82+
- [关于面试题“ArrayList循环remove()要用Iterator”的研究 - since1986](https://juejin.im/post/59ef1ab0518825619a01e00a)
83+
- [《我们一起进大厂》系列-ArrayList - 敖丙](https://juejin.im/post/5e14b51d5188253a9a213e83)
84+
- [Java集合框架常见面试题 - JavaGuide](https://github.com/Snailclimb/JavaGuide/blob/master/docs/java/collection/Java%E9%9B%86%E5%90%88%E6%A1%86%E6%9E%B6%E5%B8%B8%E8%A7%81%E9%9D%A2%E8%AF%95%E9%A2%98.md#%E8%A1%A5%E5%85%85%E5%86%85%E5%AE%B9randomaccess%E6%8E%A5%E5%8F%A3)
85+
- [Core algorithms deployed](https://cstheory.stackexchange.com/questions/19759/core-algorithms-deployed/19773#19773)
86+
- [UidGenerator][UidGenerator]
87+
88+
[anewarray]: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html#jvms-6.5.anewarray
89+
[UidGenerator]: https://github.com/baidu/uid-generator/blob/master/README.zh_cn.md

0 commit comments

Comments
 (0)