private static void genericArrayTest() {
ArrayList<? extends Exception>[] exceptionListArray = new ArrayList[10];
Object[] exceptionObjectListArray = exceptionListArray;
ArrayList<String> wrongArrayList = new ArrayList<String>();
wrongArrayList.add("String One");
ArrayList<NullPointerException> exceptionList = new ArrayList<>();
ArrayList rawList = new ArrayList();
exceptionListArray[0] = exceptionList;
//exceptionListArray[0] = wrongArrayList; // Compile error?
//exceptionListArray[0] = rawList;
exceptionObjectListArray[0] = wrongArrayList;
// No compile time error, there is no ArrayStoreException why ?
ArrayList<? extends Exception> list = exceptionListArray[0]; // No exception here as well ??
Exception e = list.get(0);
// Exception only when accessing data, why not ArrayStoreException when we stored wrong list ??
System.out.println();
}
有人可以解释发生了什么吗?
谷歌搜索说下面的数组声明是允许的,
ArrayList<Exception>[] exceptionListArray = new ArrayList[10];
但是
ArrayList<Exception>[] exceptionListArray = new ArrayList<Exception>[10];
这两个声明之间有什么区别?为什么一个不允许其他声明?
创建一个数组来存储Java中的泛型类型是在谈论如何创建泛型数组。但是这个问题不是关于如何创建通用数组,而是关于为什么在运行时不抛出ArrayStoreException以及Java中通用数组创建语法的差异
在Java中,泛型参数化没有一个将它们彼此区分的类:
ArrayList<Exception> : class is ArrayList
ArrayList<String> : class is ArrayList
这是因为泛型是使用类型Erase实现的,这意味着在编译期间,它们将被强制类型替换:
ArrayList<Exception> list = new ArrayList<Exception>();
list.add(new Exception());
Exception e = list.get(0);
变成:
ArrayList list = new ArrayList();
list.add(new Exception());
Exception e = (Exception) list.get(0);
ArrayStoreException
抛出No的原因是ArrayList<Exception>
,ArrayList<String>
在程序执行期间,它们具有相同的类型。
不允许使用通用数组,因为在执行期间无法对类型参数执行这些检查。(因为类型参数不再存在。)
当我们有这个:
ArrayList<Exception>[] a =
new ArrayList[10];
这称为未经检查的转换。a
实际上是ArrayList[]
一个数组,它存储任何内容,ArrayList
但我们基本上已经说过“假装ArrayList<Exception>[]
暂时是”。a
指向的数组对象仍然是ArrayList[]
。
请参阅以下程序进行说明:
import java.util.*;
class Example {
public static void main(String[] args) {
ArrayList<Exception> a = new ArrayList<Exception>();
ArrayList<String> b = new ArrayList<String>();
System.out.println("ArrayList<Exception> class is:");
System.out.println("\t" + a.getClass());
System.out.println("ArrayList<String> class is:");
System.out.println("\t" + b.getClass());
System.out.println(
"ArrayList<Exception> class == ArrayList<String> class:");
System.out.println("\t" + ( a.getClass() == b.getClass() ));
ArrayList<Exception>[] c = new ArrayList[0];
ArrayList<String>[] d = new ArrayList[0];
System.out.println("ArrayList<Exception>[] class is:");
System.out.println("\t" + c.getClass());
System.out.println("ArrayList<String>[] class is:");
System.out.println("\t" + d.getClass());
System.out.println(
"ArrayList<Exception>[] class == ArrayList<String>[] class:");
System.out.println("\t" + ( c.getClass() == d.getClass() ));
}
}
输出:
ArrayList<Exception> class is:
class java.util.ArrayList
ArrayList<String> class is:
class java.util.ArrayList
ArrayList<Exception> class == ArrayList<String> class:
true
ArrayList<Exception>[] class is:
class [Ljava.util.ArrayList;
ArrayList<String>[] class is:
class [Ljava.util.ArrayList;
ArrayList<Exception>[] class == ArrayList<String>[] class:
true
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句