|
@@ -14,7 +14,7 @@ ArrayList:源码难点 扩容和缩容
|
|
|
|
|
|
## 扩容方法
|
|
|
|
|
|
-```
|
|
|
+```java
|
|
|
private void ensureExplicitCapacity(int minCapacity) {
|
|
|
// 线程同步,不能在遍历的时候进行添加或者删除操作
|
|
|
modCount++;
|
|
@@ -25,7 +25,7 @@ private void ensureExplicitCapacity(int minCapacity) {
|
|
|
}
|
|
|
```
|
|
|
|
|
|
-```
|
|
|
+```java
|
|
|
private void grow(int minCapacity) {
|
|
|
// 初始化容量为0
|
|
|
int oldCapacity = elementData.length;
|
|
@@ -48,7 +48,7 @@ private void grow(int minCapacity) {
|
|
|
|
|
|
注意hashmap集合没有缩容,因为缩容需要重新计算index值
|
|
|
基于value值删除复杂度为O(n),需要考虑缩容问题
|
|
|
-```
|
|
|
+```java
|
|
|
public boolean remove(Object o) {
|
|
|
if (o == null) {
|
|
|
for (int index = 0; index < size; index++)
|
|
@@ -70,7 +70,7 @@ public boolean remove(Object o) {
|
|
|
|
|
|
## 快速覆盖
|
|
|
|
|
|
-```
|
|
|
+```java
|
|
|
private void fastRemove(int index) {
|
|
|
// 线程同步,不能在遍历的时候进行添加或者删除操作
|
|
|
modCount++;
|