实例化后的list的默认值
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
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at test.test.List结构.List默认值.main(List默认值.java:35)