- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
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];
这两个声明之间有什么区别?为什么允许一个而不允许另一个?
Creating an array to store generic types in Java正在谈论如何创建通用数组。但是这个问题不是关于如何创建通用数组,而是关于为什么在运行时不抛出 ArrayStoreException 以及 java 中通用数组创建语法的差异
最佳答案
在 Java 中,泛型参数化没有类来区分它们:
ArrayList<Exception> : class is ArrayList
ArrayList<String> : class is ArrayList
这是因为泛型是用 type erasure 实现的,这意味着在编译期间,它们被转换为替换:
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() ));
}
}
输出:
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
关于java - 为什么下面的代码没有抛出 ArrayStoreException?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30014274/
为什么下面的代码会抛出ArrayStoreException? double[] a = {2.0,3.4,3.6,2.7,5.6}; int[] b = {2,3,4,5}; System.arra
当我使用 Junit 进行测试时,我似乎无法运行此测试。它仅在失败跟踪上显示错误和“java.lang.ArrayStoreException”。如果有人能解决我的问题,那将是一个很大的帮助。 Lin
下面是我的代码,用于将字符串转换为其等效的 ascii 字符串。该字符串将只包含数字 - 这就是为什么我为每个字符分配 2 个字节(因为 1 到 9 在 ascii 中分别是 49 到 58)但我收到
private static void genericArrayTest() { ArrayList[] exceptionListArray = new ArrayList[10];
我有一个奇怪的问题。我有三个类(class)。书籍(抽象)和两个子类(小说、非小说)。我创建了书籍引用数组。该数组可以包含两个子类的对象。我有以下 Fiction 类代码。 public class
我读过很多关于 ArrayStoreExceptions 的帖子,其中大部分都属于以下类别之一: 基本误解(将基类放入子类的数组声明中) 在从列表转换为数组时出现类似的错误 围绕数组与泛型的“协变”问
我正在尝试创建一个链表数组来处理散列值冲突。我有一个对象数组,我试图存储 linkedList 但它似乎不起作用。我做错了什么,或者我应该采取更有效的方法。 这是我在运行代码时遇到的错误。 Excep
考虑以下对 Java 的 ArrayList#toArray 方法的测试。请注意,我借用了这个有用的代码 answer . public class GenericTest { public
当我遇到这样的问题时,我正在处理继承。我的代码如下 public class Parent { public void methodParent() {
Object[] o = "a;b;c".split(";"); o[0] = 42; 抛出 java.lang.ArrayStoreException: java.lang.Integer 同时 S
为什么我会收到此异常? java.lang.ArrayStoreException: java.lang.Integer at MyLinkedList.toArray(MyLinkedList.ja
我确实有这样一段代码: List userIdentifiers = getUsersModule().getUsersIdentifiers(); if (userIdentifiers.isEmp
这是Core Java一书中的例子 两类员工和经理。经理扩展员工。下面的代码 Manager[] mans ={new Manager("Adam"),new Manager("Ben")};
我正在存储对 BiConsumers 的引用适应Consumer : public void setConsumer(BiConsumer consumer) { fieldConsumer
我正在尝试运行一个基本的 JUNG 图示例 - 我能够在独立的 java 程序中运行以下代码,但是当我将相同的代码放入使用 Maven 构建的服务器端 jar 中时,它失败了Visualization
我的理解是你会得到 ArrayStoreException当您尝试插入不兼容类型的对象时。 Object[] array = new String[1]; array[0] = 1; 有人可以举个例子
您好,当我尝试将实现接口(interface)的类存储到定义为接口(interface)数组的数组中时,我收到此 Java ArrayStoreException 异常。代码如下: private I
每当我尝试使用 JPA 2.0 和 Websphere 运行更新查询时,都会收到 java.lang.ArrayStoreException。我正在使用测试实用程序,并且成功更新了正确的行等等,没有事
我正在创建一个类,其中包含名为 array 的 Object[] 类型的实例变量。此类包含返回 T[] 的方法,其中 T 是泛型。该方法应将 array 的内容复制到要返回的数组中。相关代码如下所示:
我的问题是,为什么我在线程“main”java.lang.ArrayStoreException 中收到异常: at java.lang.System.arraycopy (native method
我是一名优秀的程序员,十分优秀!