mirror of
https://github.com/Snailclimb/JavaGuide
synced 2025-08-01 16:28:03 +08:00
Compare commits
4 Commits
3501c6426b
...
1def2a3711
Author | SHA1 | Date | |
---|---|---|---|
|
1def2a3711 | ||
|
77e72b926e | ||
|
6d0fc7cbdb | ||
|
e54f5ea964 |
@ -159,19 +159,22 @@ public class ArrayList<E> extends AbstractList<E>
|
|||||||
* @param minCapacity 所需的最小容量
|
* @param minCapacity 所需的最小容量
|
||||||
*/
|
*/
|
||||||
public void ensureCapacity(int minCapacity) {
|
public void ensureCapacity(int minCapacity) {
|
||||||
//如果是true,minExpand的值为0,如果是false,minExpand的值为10
|
// 如果不是默认空数组,则minExpand的值为0;
|
||||||
|
// 如果是默认空数组,则minExpand的值为10
|
||||||
int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
|
int minExpand = (elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA)
|
||||||
// any size if not default element table
|
// 如果不是默认元素表,则可以使用任意大小
|
||||||
? 0
|
? 0
|
||||||
// larger than default for default empty table. It's already
|
// 如果是默认空数组,它应该已经是默认大小
|
||||||
// supposed to be at default size.
|
|
||||||
: DEFAULT_CAPACITY;
|
: DEFAULT_CAPACITY;
|
||||||
//如果最小容量大于已有的最大容量
|
|
||||||
|
// 如果最小容量大于已有的最大容量
|
||||||
if (minCapacity > minExpand) {
|
if (minCapacity > minExpand) {
|
||||||
|
// 根据需要的最小容量,确保容量足够
|
||||||
ensureExplicitCapacity(minCapacity);
|
ensureExplicitCapacity(minCapacity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// 根据给定的最小容量和当前数组元素来计算所需容量。
|
// 根据给定的最小容量和当前数组元素来计算所需容量。
|
||||||
private static int calculateCapacity(Object[] elementData, int minCapacity) {
|
private static int calculateCapacity(Object[] elementData, int minCapacity) {
|
||||||
// 如果当前数组元素为空数组(初始情况),返回默认容量和最小容量中的较大值作为所需容量
|
// 如果当前数组元素为空数组(初始情况),返回默认容量和最小容量中的较大值作为所需容量
|
||||||
@ -429,8 +432,7 @@ public class ArrayList<E> extends AbstractList<E>
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Private remove method that skips bounds checking and does not
|
* 该方法为私有的移除方法,跳过了边界检查,并且不返回被移除的值。
|
||||||
* return the value removed.
|
|
||||||
*/
|
*/
|
||||||
private void fastRemove(int index) {
|
private void fastRemove(int index) {
|
||||||
modCount++;
|
modCount++;
|
||||||
@ -438,7 +440,7 @@ public class ArrayList<E> extends AbstractList<E>
|
|||||||
if (numMoved > 0)
|
if (numMoved > 0)
|
||||||
System.arraycopy(elementData, index + 1, elementData, index,
|
System.arraycopy(elementData, index + 1, elementData, index,
|
||||||
numMoved);
|
numMoved);
|
||||||
elementData[--size] = null; // clear to let GC do its work
|
elementData[--size] = null; // 在移除元素后,将该位置的元素设为 null,以便垃圾回收器(GC)能够回收该元素。
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -322,7 +322,7 @@ JDK1.2 以后,Java 对引用的概念进行了扩充,将引用分为强引
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
关于具体是标记可回收对象还是不可回收对象,众说纷纭,两种说法其实都没问题,我个人更倾向于是前者。
|
关于具体是标记可回收对象(不可达对象)还是不可回收对象(可达对象),众说纷纭,两种说法其实都没问题,我个人更倾向于是后者。
|
||||||
|
|
||||||
如果按照前者的理解,整个标记-清除过程大致是这样的:
|
如果按照前者的理解,整个标记-清除过程大致是这样的:
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user