list clear() 和 =[] 和 =null 问题
- clear() 释放list 里面所有对象 ,不管是哪个实现类的clear 方式都是将里面的所有元素都释放了并且清空里面的属性,List对象是存在的
- ArrayList 源码如下
public void clear() {
modCount++;
// clear to let GC do its work
for (int i = 0; i < size; i++)
elementData[i] = null;
size = 0;
}
public void clear() {
// Clearing all of the links between nodes is "unnecessary", but:
// - helps a generational GC if the discarded nodes inhabit
// more than one generation
// - is sure to free memory even if there is a reachable Iterator
for (Node<E> x = first; x != null; ) {
Node<E> next = x.next;
x.item = null;
x.next = null;
x.prev = null;
x = next;
}
first = last = null;
size = 0;
modCount++;
}
- = null 释放数组对象,里面所引用的对象也就释放了。
- = [] 数组对象的引用指向一个空数组。