ArrayList源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
public class MyArrayList<E> implements Iterable<E> {

private static final int DEFAULT_CAPACITY = 10; //初始容量

transient Object[] elementData;

//空数组实例
private static final Object[] EMPTY_ELEMENTDATA = {};

//默认空数组实例,以了解添加第一个元素时,空间需要扩大多少
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};

//与快速失败机制有关的属性,记录容器大小是否更改
protected transient int modCount = 0;

//虚拟机规定的最大数组大小,超过可能会引发OutOfMemoryError
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;

//数组大小
private int size;

public MyArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}


public MyArrayList(int initialCapacity) {
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("容量错误");
}
}

public void add(int index, E element) {
//todo
}

public boolean add(E e) {
//保证容量
ensureCapacityInternal(size + 1);
elementData[size++] = e;
return true;

}

private static int calculateCapacity(Object[] elementData, int minCapacity) {
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
return Math.max(DEFAULT_CAPACITY, minCapacity);
}
return minCapacity;

}


private void ensureCapacityInternal(int minCapacity) {
ensureExplicitCapacity(calculateCapacity(elementData, minCapacity));
}

private void ensureExplicitCapacity(int minCapacity) {
modCount++;
if (minCapacity - elementData.length > 0) {
grow(minCapacity);
}
}

//扩容核心方法
private void grow(int minCapacity) {
int oldCapacity = elementData.length;
//这边右移一位表示一半,所以扩容为原来的1.5大小
int newCapacity = oldCapacity + (oldCapacity >> 1);
if (newCapacity - minCapacity < 0) {
newCapacity = minCapacity;
}
if (newCapacity - MAX_ARRAY_SIZE > 0) {
newCapacity = hugeCapacity(minCapacity);
}
//复制扩容数组
elementData = Arrays.copyOf(elementData, newCapacity);
}

public boolean remove(Object o) {
if (o == null) {
for (int index = 0; index < size; index++) {
if (elementData[index] == null) {
fastRemove(index);
return true;
}
}
} else {
for (int index = 0; index < size; index++) {
if (o.equals(elementData[index])) {
fastRemove(index);
return true;
}
}
}

return false;
}

public void fastRemove(int index) {
modCount++;
int numMoved = size - index - 1;
if (numMoved > 0) {
System.arraycopy(elementData, index + 1, elementData, index, numMoved);
}
elementData[--size] = null;
}


private static int hugeCapacity(int minCapaCity) {
if (minCapaCity < 0) {
throw new OutOfMemoryError();
}
return minCapaCity > MAX_ARRAY_SIZE ? Integer.MAX_VALUE : MAX_ARRAY_SIZE;
}

//返回迭代器
@Override
public Iterator<E> iterator() {
return null;
}

//内部迭代器
private class Itr implements Iterator<E> {
int curosr; //下一个元素index
int lastRet = -1; // 最后一个元素的index;

int expectedModCount = modCount; //快速失败机制

Itr() {
}

@Override
public boolean hasNext() {
return curosr != size;
}

@Override
public E next() {
checkForComodification();
int i = curosr;
if (i >= size) {
throw new NoSuchElementException();
}

Object[] elementData = MyArrayList.this.elementData;
if (i >= elementData.length) {
throw new ConcurrentModificationException();
}
curosr = i + 1;
return (E) elementData[lastRet = i];

}

@Override
public void remove() {
if (lastRet < 0) {
throw new IllegalStateException();
}
checkForComodification();
MyArrayList.this.remove(lastRet);
curosr = lastRet;
lastRet = -1;
expectedModCount = modCount;

}

private void checkForComodification() {
if (modCount != expectedModCount) {
throw new ConcurrentModificationException();
}
}
}
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!