Java 为什么在下面的代码中没有抛出ArrayStoreException? private static void genericarrytest(){ ArrayList
在Java中,泛型参数化没有一个类可以将它们区别开来:Java 为什么在下面的代码中没有抛出ArrayStoreException? private static void genericarrytest(){ ArrayList,java,arrays,generics,Java,Arrays,Generics,在Java中,泛型参数化没有一个类可以将它们区别开来: private static void genericArrayTest() { ArrayList<? extends Exception>[] exceptionListArray = new ArrayList[10]; Object[] exceptionObjectListArray = exceptionListArray; ArrayList<String> wrongAr
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();
}
不会引发任何ArrayStoreException
,因为ArrayList
和ArrayList
在程序执行期间具有相同的类型
不允许使用泛型数组,因为在执行期间无法对类型参数执行这些检查。(因为类型参数不再存在。)
当我们有了这个:
ArrayList list = new ArrayList();
list.add(new Exception());
Exception e = (Exception) list.get(0);
输出:
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类是:
类java.util.ArrayList
ArrayList类是:
类java.util.ArrayList
ArrayList类==ArrayList类:
真的
ArrayList[]类是:
类[Ljava.util.ArrayList;
ArrayList[]类是:
类[Ljava.util.ArrayList;
ArrayList[]类==ArrayList[]类:
真的
在这里查看您的最后一个问题:。关于exceptionObjectListArray,它被声明为对象类型数组。因此,任何扩展对象类型的数组都可以分配给它。@giorashc从上面的链接,我进入了-。它说“如果允许参数化列表数组,代码将无法抛出所需的ArrayStoreException”但是,如果上面的代码块正确的话,这种情况现在也会发生。因此,问题是java通过允许'ArrayList[]exceptionListArray=new ArrayList[10]实现了什么“;”?的可能重复我不认为这是完全重复的,因为这个问题问的是ArrayStoreException。键入erasure与否,Java中的每个类都是从对象
扩展而来。我想这足以解释为什么不抛出ArrayStoreException
。@ChetanKinger我真的不明白这有什么关系。@ChetanKinger不相关,因为exceptionObjectListArray
的类是ArrayList[]
,而不是Object[]
。对于编译时类型为Object[]的数组,可以获取ArrayStoreException
@pbabcdefp您能否详细说明一下您所说的异常对象列表数组的类是ArrayList[]
?您的意思是在运行时,因为在编译时,它是一个对象[]
数组,对吗?@ChetanKinger Try
ArrayList list = new ArrayList();
list.add(new Exception());
Exception e = (Exception) list.get(0);
ArrayList<Exception>[] a =
new ArrayList[10];
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