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