日期:2014-05-20  浏览次数:20737 次

数组移除一个对象,如果gc运行,是否会回收该对象,出现null异常
Java code

        
        
        ArrayList<Shop> shopList =getShopList();
        Shop shop =  shopList.get(shopList.size()-1);
        shopList.remove(0);
        [color=#FF0000]System.out.println(shop);[/color]



是不是会出现null异常

------解决方案--------------------
不会, 因为还有引用指向该对象,所以不会被gc掉
------解决方案--------------------
回收是不可操控的
------解决方案--------------------
ArrayList<Shop> shopList =getShopList();
Shop shop = shopList.get(shopList.size()-1);
shopList.remove(0);
数组移除一个对象,如果gc运行,是否会回收该对象(有可能会回收)
System.out.println(shop);
是不是会出现null异常(不会。即使是null对象也只是打印出null,除非你对null对象进行操作)





------解决方案--------------------
不可能出现
只要 ArrayList<Shop> shopList =getShopList();
shopList 指向的list不为null
你的代码就不会出现nullpointexception();
最多就是IndexOutOfBoundsException;
gc不会对强引用可达的对象进行回收
------解决方案--------------------
impossibility


------解决方案--------------------
不会,但是看了你的代码,会出现异常的。如果list为空,你的remove和get方法就报错啦
------解决方案--------------------
探讨

不会, 因为还有引用指向该对象,所以不会被gc掉