1. ArrayList() 会使用长度为零的数组
2. ArrayList(int initialCapacity) 会使用指定容量的数组
3. public ArrayList(Collection extends E> c) 会使用 c 的大小作为数组容量
4. add(Object o) 首次扩容为 10,再次扩容为上次容量的 1.5 倍
5. addAll(Collection c) 没有元素时,扩容为 Math.max(10, 实际元素个数),有元素时为 Math.max(原容量 1.5 倍, 实际元素个数)
扩容方式不是简单的*1.5倍
public
class
TestArrayList {
public
static
void
main(String[] args) {
System.out.println(arrayListGrowRule(
30
));
// testAddAllGrowEmpty();
testAddAllGrowNotEmpty();
}
private
static
List arrayListGrowRule(
int
n) {
List list =
new
ArrayList<>();
int
init =
0
;
list.add(init);
if
(n >=
1
) {
init =
10
;
list.add(init);
}
for
(
int
i =
1
; i < n; i++) {
init += (init) >>
1
;
list.add(init);
}
return
list;
}
// addAll(Collection c) 没有元素时,扩容为 Math.max(10, 实际元素个数),
// 有元素时为 Math.max(原容量 1.5 倍, 实际元素个数)
private
static
void
testAddAllGrowEmpty() {
ArrayList list =
new
ArrayList<>();
// list.addAll(Arrays.asList(1, 2, 3));// 10
list.addAll(Arrays.asList(
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
,
10
,
11
));
// 11
System.out.println(length(list));
}
// addAll(Collection c) 没有元素时,扩容为 Math.max(10, 实际元素个数),
// 有元素时为 Math.max(原容量 1.5 倍, 实际元素个数)
private
static
void
testAddAllGrowNotEmpty() {
ArrayList list =
new
ArrayList<>();
for
(
int
i =
0
; i <
10
; i++) {
list.add(i);
}
// list里面已经有10个元素
// list.addAll(Arrays.asList(1, 2, 3));// max(15,10)=15
list.addAll(Arrays.asList(
1
,
2
,
3
,
4
,
5
,
6
));
// max(15,16)=16
System.out.println(length(list));
}
public
static
int
length(ArrayList list) {
try
{
Field field = ArrayList.
class
.getDeclaredField(
"elementData"
);
field.setAccessible(
true
);
return
((Object[]) field.get(list)).length;
}
catch
(Exception e) {
e.printStackTrace();
return
0
;
}
}
}