zoukankan      html  css  js  c++  java
  • list clear() 和 =[] 和 =null 问题

    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;
        }
    
    • LinkedList 源码如下
        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 释放数组对象,里面所引用的对象也就释放了。
    • = [] 数组对象的引用指向一个空数组。
  • 相关阅读:
    自定义长时间定时器对象
    poj1326
    poj1323
    poj1218
    poj1298
    poj1276
    新年的第一场雪
    Java 语言学习总结
    假使时光能够倒转
    为了回家——春运3日战纪实
  • 原文地址:https://www.cnblogs.com/jsersudo/p/13444453.html
Copyright © 2011-2022 走看看