创建一个数组来存储Java中的泛型类型 [英] Creating an array to store generic types in Java

查看:106
本文介绍了创建一个数组来存储Java中的泛型类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我必须创建一个数组来存储整数的ArrayList,并且数组大小为10。

下面的代码将执行此操作:

  ArrayList< Integer> [] pl2 = new ArrayList [10]; 

问题1:

在我看来,更合适的代码是

  ArrayList< Integer> [] pl2 = new ArrayList< Integer> [10]; 

为什么这不起作用?

问题2:



以下两种编译


  1. ArrayList< Integer> [] pl2 = new ArrayList [10];

  2. ArrayList [] pl3 = new ArrayList [10];

参考声明的区别是什么? c $ c> pl2 和 pl3 是否值得关注?

解决方案 div>

问题1:

基本上,这是Java语言禁止的。这包括在泛型的Java语言规范中。



当您使用

  ArrayList< Integer> [] pl2 = new ArrayList [10]; //警告

你会得到编译器警告,因为下面的例子会编译(为每行产生警告):

$ $ $ $ $ $ $ $ $ $ $ ArrayList wrongRawArrayList = new ArrayList(); //警告
wrongRawArrayList.add(string1); //警告
wrongRawArrayList.add(string2); //警告

pl2 [0] = wrongRawArrayList; //警告

但现在你的数组,应该包含 ArrayList 整数,包含完全错误 的ArrayList 字符串

问题2:



p12 声明为您提供编译时检查,并在从 ArrayList 中获取项目时使用cast来释放。

稍微修改过的例子:

  ArrayList< Integer> [] pl2 = new ArrayList [10]; //警告

ArrayList< String> wrongArrayList = new ArrayList< String>(); // 好!
wrongArrayList.add(string1); // 好!
wrongArrayList.add(string2); // 好!

pl2 [0] = wrongArrayList; //错误

现在,由于您使用的是泛型,因此无法编译。
但是如果你使用

$ pre $ Array $ [] pl2 = new ArrayList [10];

您将获得与第一个示例中相同的结果。


Suppose I have to create an array which stores ArrayList's of Integers and the array size is 10.

The below code will do it:

ArrayList<Integer>[] pl2 = new ArrayList[10]; 

Question 1:

In my opinion the more appropriate code would be

ArrayList<Integer>[] pl2 = new ArrayList<Integer>[10];    

Why does this not work?

Question 2:

Both of the below compile

  1. ArrayList<Integer>[] pl2 = new ArrayList[10];
  2. ArrayList[] pl3 = new ArrayList[10];

What is the difference as far as the reference declaration of pl2 and pl3 is concerned?

解决方案

Question 1:

Basically, this is forbidden by Java language. This is covered in Java Language Specification for generics.

When you use

ArrayList<Integer>[] pl2 = new ArrayList[10];    // warning

you get the compiler warning, because the following example will compile (generating warning for every line of code):

ArrayList wrongRawArrayList = new ArrayList();      // warning
wrongRawArrayList.add("string1");                   // warning 
wrongRawArrayList.add("string2");                   // warning  

pl2[0] = wrongRawArrayList;                         // warning 

but now you array, that supposed to contain ArrayList of Integer, contains totally wrong ArrayList of String objects.

Question 2:

As it was already answered, declaration of p12 provides you with compile time checking and frees you from using casting when getting items from your ArrayList.

Slightly modified previous example:

ArrayList<Integer>[] pl2 = new ArrayList[10];                // warning 

ArrayList<String> wrongArrayList = new ArrayList<String>();  // OK!
wrongArrayList.add("string1");                               // OK! 
wrongArrayList.add("string2");                               // OK!

pl2[0] = wrongArrayList;                                     // ERROR

Now, since you are using generics, this won't compile. But if you use

ArrayList[] pl2 = new ArrayList[10]; 

you will get the same result as in the first example.

这篇关于创建一个数组来存储Java中的泛型类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆