美文网首页
实例化后的list的默认值

实例化后的list的默认值

作者: kangkaii | 来源:发表于2017-09-13 11:10 被阅读1181次
    public class List默认值 {
        public static void main(String[] args) {
            List<String> arrayList = new ArrayList<>();
            List<String> linkedList = new LinkedList<>();
            if (arrayList.isEmpty()) {
                System.out.println("arrayList默认是空");
            }
            if (linkedList.isEmpty()) {
                System.out.println("linkedList默认是空");
            }
            if (arrayList != null) {
                System.out.println("arrayList默认不是null");
            }
            if (linkedList != null) {
                System.out.println("linkedList默认不是null");
            }
            if (arrayList.size() == 0) {
                System.out.println("arrayList.size()是0");
            }
            if (linkedList.size() == 0) {
                System.out.println("linkedList.size()是0");
            }
            try {
                System.out.println(arrayList.get(0)+"arrayList.get(0)没抛异常");
            }catch (Exception e){
                System.out.println(arrayList.get(0)+"arrayList.get(0)抛了异常");
            }
            try {
                System.out.println(linkedList.get(0)+"linkedList.get(0)没抛异常");
            }catch (Exception e){
                System.out.println(linkedList.get(0)+"linkedList.get(0)抛了异常");
            }
        }
    }
    

    结果为:

    arrayList默认是空
    linkedList默认是空
    arrayList默认不是null
    linkedList默认不是null
    arrayList.size()是0
    linkedList.size()是0
    arrayList.get(0)抛了异常
    linkedList.get(0)抛了异常

    相关文章

      网友评论

          本文标题:实例化后的list的默认值

          本文链接:https://www.haomeiwen.com/subject/eokksxtx.html