通用列表数组 [英] Array of Generic List

查看:64
本文介绍了通用列表数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ArrayList< Key>< p> a = new ArrayList< Key>(); 

但是编译器抱怨这个,

  ArrayList< Key> [] a = new ArrayList< Key> [10]; 

通过在stackoverflow中阅读文章,我理解这是由于Type Erasure,我可以修复它通过使用,

  ArrayList< Key> [] a =(ArrayList< Key> [])new ArrayList [10]; 

或列表清单

 的ArrayList< ArrayList的<钥匙及GT;> b = new ArrayList< ArrayList< Key>>(); 

但我无法弄清楚幕后的原因。特别是,为什么第二个是非法的,因为第一个是非常好的。为什么编译器不会抱怨列表的列表。

解决方案

你不能拥有一个数组,因为一个数组需要原始类型。你在第二个实例中对它进行了类型转换,这使它符合定义的类型,因此是合法的(但是,这是不可能的)。列表的列表是合法的 ArrayList 不是数组。



阅读第7.3章(第15页) 官方教程了解更多详情。


数组对象的组件类型可能不是类型变量或
参数化类型,除非它是(无界)通配符类型。您可以
声明其元素类型为类型变量或
参数化类型但不包含数组对象的数组类型。
这很烦人,当然。此限制对于避免以下情况是必要的:

  List< String> [] lsa = new List< String> [10]; //不是真的允许
Object o = lsa;
Object [] oa =(Object [])o;
列表<整数> li = new ArrayList< Integer>();
li.add(new Integer(3));
oa [1] = li; //不合格,但通过运行时存储检查
String s = lsa [1] .get(0); //运行时错误 - ClassCastException

如果允许使用参数化类型的数组,

本教程继续说明如下内容:b $ b编译时没有任何未经检查的警告,但在运行时失败。


由于类型变量在运行时不存在,因此无法确定
实际数组类型是什么。
解决这些限制的方法是使用类文字作为运行时间
类型的令牌



I am playing with Generic and arrays, it seems the following code compiles fine,

ArrayList<Key> a = new ArrayList<Key>();

But the compiler complains about this one,

ArrayList<Key>[] a = new ArrayList<Key>[10];

By reading post in stackoverflow, I sort of understand that this is due to Type Erasure and I can fix it by using,

ArrayList<Key>[] a = (ArrayList<Key> []) new ArrayList[10];

or list of list

ArrayList<ArrayList<Key>> b = new ArrayList<ArrayList<Key>>();

But I can't figure out the reason behind the scene. Especially, why the second one is illegal given the first one is perfectly OK. And why the compiler does not complain about the list of list.

解决方案

You can't have an array, because an array requires a raw type. You typecast it in the second instance, which makes it fit the defined type, and is therefore legal (however, this is impossible for it to infer). The list of list is legal as ArrayList isn't an array.

Read chapter 7.3 (page 15) in the official tutorial for more details on this.

The component type of an array object may not be a type variable or a parameterized type, unless it is an (unbounded) wildcard type.You can declare array types whose element type is a type variable or a parameterized type, but not array objects. This is annoying, to be sure. This restriction is necessary to avoid situations like:

List<String>[] lsa = new List<String>[10]; // not really allowed
Object o = lsa;
Object[] oa = (Object[]) o;
List<Integer> li = new ArrayList<Integer>();
li.add(new Integer(3));
oa[1] = li; // unsound, but passes run time store check
String s = lsa[1].get(0); // run-time error - ClassCastException

If arrays of parameterized type were allowed, the example above would compile without any unchecked warnings, and yet fail at run-time.

The tutorial then goes on to say the following:

Since type variables don’t exist at run time, there is no way to determine what the actual array type would be. The way to work around these kinds of limitations is to use class literals as run time type tokens

这篇关于通用列表数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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