首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在代码下面没有ArrayStoreException?

为什么在代码下面没有ArrayStoreException?
EN

Stack Overflow用户
提问于 2015-05-03 13:17:52
回答 1查看 187关注 0票数 2
代码语言:javascript
复制
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中泛型数组创建语法的差异。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-03 13:35:09

在Java中,泛型参数化没有一个类来区分它们:

代码语言:javascript
复制
ArrayList<Exception> : class is ArrayList
ArrayList<String>    : class is ArrayList

这是因为泛型是用类型擦除实现的,这意味着在编译期间,它们被转换为:

代码语言:javascript
复制
ArrayList<Exception> list = new ArrayList<Exception>();
list.add(new Exception());
Exception e = list.get(0);

变成:

代码语言:javascript
复制
ArrayList list = new ArrayList();
list.add(new Exception());
Exception e = (Exception) list.get(0);

没有引发ArrayStoreException,因为ArrayList<Exception>ArrayList<String>在程序执行期间具有相同的类型。

不允许使用泛型数组,因为在执行过程中无法对类型参数执行这些检查。(因为类型参数不再存在。)

当我们有了这个:

代码语言:javascript
复制
ArrayList<Exception>[] a =
    new ArrayList[10];

这叫做未经检查的转换。a实际上是一个ArrayList[],一个存储任何ArrayList的数组,但我们基本上已经说过“暂时假装它是一个ArrayList<Exception>[]”。a指向的数组对象仍然是ArrayList[]

请参阅下面的程序来说明:

代码语言:javascript
复制
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

输出:

代码语言:javascript
复制
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
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30014274

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档