@@ -159,19 +159,22 @@ public class ArrayList<E> extends AbstractList<E>
159
159
* @param minCapacity 所需的最小容量
160
160
*/
161
161
public void ensureCapacity (int minCapacity ) {
162
- // 如果是true,minExpand的值为0,如果是false,minExpand的值为10
162
+ // 如果不是默认空数组,则minExpand的值为0;
163
+ // 如果是默认空数组,则minExpand的值为10
163
164
int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA )
164
- // any size if not default element table
165
+ // 如果不是默认元素表,则可以使用任意大小
165
166
? 0
166
- // larger than default for default empty table. It's already
167
- // supposed to be at default size.
167
+ // 如果是默认空数组,它应该已经是默认大小
168
168
: DEFAULT_CAPACITY ;
169
- // 如果最小容量大于已有的最大容量
169
+
170
+ // 如果最小容量大于已有的最大容量
170
171
if (minCapacity > minExpand) {
172
+ // 根据需要的最小容量,确保容量足够
171
173
ensureExplicitCapacity(minCapacity);
172
174
}
173
175
}
174
176
177
+
175
178
// 根据给定的最小容量和当前数组元素来计算所需容量。
176
179
private static int calculateCapacity (Object [] elementData , int minCapacity ) {
177
180
// 如果当前数组元素为空数组(初始情况),返回默认容量和最小容量中的较大值作为所需容量
@@ -429,16 +432,15 @@ public class ArrayList<E> extends AbstractList<E>
429
432
}
430
433
431
434
/*
432
- * Private remove method that skips bounds checking and does not
433
- * return the value removed.
435
+ * 该方法为私有的移除方法,跳过了边界检查,并且不返回被移除的值。
434
436
*/
435
437
private void fastRemove (int index ) {
436
438
modCount++ ;
437
439
int numMoved = size - index - 1 ;
438
440
if (numMoved > 0 )
439
441
System . arraycopy(elementData, index + 1 , elementData, index,
440
442
numMoved);
441
- elementData[-- size] = null ; // clear to let GC do its work
443
+ elementData[-- size] = null ; // 在移除元素后,将该位置的元素设为 null,以便垃圾回收器(GC)能够回收该元素。
442
444
}
443
445
444
446
/**
0 commit comments