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();
}有人能解释一下发生了什么事吗?
Google说下面的数组声明是允许的
ArrayList<Exception>[] exceptionListArray = new ArrayList[10];
但不是这个
ArrayList<Exception>[] exceptionListArray = new ArrayList<Exception>[10];
这两种声明之间有什么区别&为什么一条不允许另一条?
创建数组以存储Java中的泛型类型正在讨论如何创建泛型数组。但是这个问题不是关于如何创建泛型数组,而是为什么在运行时不抛出ArrayStoreException & java中泛型数组创建语法的差异。
发布于 2015-05-03 13:35:09
在Java中,泛型参数化没有一个类来区分它们:
ArrayList<Exception> : class is ArrayList
ArrayList<String> : class is ArrayList这是因为泛型是用类型擦除实现的,这意味着在编译期间,它们被转换为:
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,因为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() ));
}
}http://ideone.com/dfCZjy
输出:
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:
truehttps://stackoverflow.com/questions/30014274
复制相似问题