Java泛型 - ArrayList的初始化 [英] Java generics - ArrayList initialization

查看:149
本文介绍了Java泛型 - ArrayList的初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,数组列表初始化。应该是这样的。

 的ArrayList< A>一个=新的ArrayList< A>();
ArrayList的<整数GT;一个=新的ArrayList<数字与GT;(); //编译时错误

那么,为什么java的允许这些?

  1。 ArrayList的&LT ;?扩展对象> A1 =新的ArrayList<对象>();
2. ArrayList的<> A2 =新的ArrayList<整数GT;();

那么,如果他们是正确的,为什么不允许这些?

  1。 a1.add(3);
2. a2.add(3);

编译器消息是:Add方法的类型ArrayList是不适用的参数(INT)

(INT,捕捉#1的扩展对象?)

更一般

  1 a1.add(空E);
  2. a2.add(五);

我读到这一点,但它是很好的听到你的声音。谢谢

另一个有趣的一点是:

 的ArrayList< ArrayList的<>>一个=新的ArrayList< ArrayList的<>>(); //正确
 ArrayList的<>一个=新的ArrayList<>(); //错了。我知道这是原因,但我有一些
在我的脑海里,上面提到的问题


解决方案

您不能分配列表<数> 来类型的引用列表<整数GT; ,因为列表<数> 允许类型比整数其他号码的 。如果你被允许这样做,以下将被允许:

 列表<数字与GT;数=新的ArrayList<数字与GT;();
numbers.add(1.1); //添加一个双
清单<整数GT;整数=数;
整型失败= ints.get(0); // ClassCastException异常!

类型列表<整数GT; 正在作出保证,任何它包含将是一个整数。这就是为什么你被允许得到一个整数出它无需进行转换。正如你所看到的,如果编译器允许的另一种类型的列表编号来分配给列表与LT;整数方式> 的担保,将被打破。

指定一个列表<整数GT; 来的类型,如列表℃的参考;> 名单,LT ;?扩展号码> 是合法的,因为 表示(其中类型为对象给定类型的一些未知子类型? 在短短编号中的情况下,?延伸数)。

由于表示的不知道的哪些对象的特定类型的列表会接受,这是不合法添加任何东西,但它。您,但是,允许的检索的从它的任何对象,它是使用的目的是什么?扩展X 界通配符类型。需要注意的是正好相反的?超级X 界通配符类型...一个名单,LT ;?超级整数GT; 是一些未知类型的至少整数的超类型的列表。虽然你不知道到底是什么类型的列表的是(可能是列表<整数GT; 列表<数> 列表<对象> ),你就知道肯定不管它是什么,一个整数可以添加到它。

最后,新的ArrayList<>()是不合法的,因为当你创建像 A paramterized类的一个实例的ArrayList ,你必须给一个的具体的类型参数。你真的可以使用,因为你会无论在你的榜样(对象,也没关系)从不能够什么,但因为你赋予它直接给它添加到的ArrayList<> 参考

It is known that arraylist init. should be like this

ArrayList<A> a = new ArrayList<A>();
ArrayList<Integer> a = new ArrayList<Number>(); // compile-time error

so, why does java allow these ?

1. ArrayList<? extends Object> a1 = new ArrayList<Object>();
2. ArrayList<?> a2 = new ArrayList<Integer>();

then, if they are correct why doesn't allow these ?

1. a1.add(3);
2. a2.add(3);

the compiler message is : The method add(int, capture#1-of ? extends Object) in the type ArrayList is not applicable for the arguments (int)

more general

  1. a1.add(null e);
  2. a2.add(? e);

I read about this but it is good to hear from you. thanks

the other amusing point is :

 ArrayList<ArrayList<?>> a = new ArrayList<ArrayList<?>>(); // correct
 ArrayList<?> a = new ArrayList<?>(); // wrong. I know it's reason but I have some 
question in my mind that mentioned above 

解决方案

You can't assign a List<Number> to a reference of type List<Integer> because List<Number> allows types of numbers other than Integer. If you were allowed to do that, the following would be allowed:

List<Number> numbers = new ArrayList<Number>();
numbers.add(1.1); // add a double
List<Integer> ints = numbers;
Integer fail = ints.get(0); // ClassCastException!

The type List<Integer> is making a guarantee that anything it contains will be an Integer. That's why you're allowed to get an Integer out of it without casting. As you can see, if the compiler allowed a List of another type such as Number to be assigned to a List<Integer> that guarantee would be broken.

Assigning a List<Integer> to a reference of a type such as List<?> or List<? extends Number> is legal because the ? means "some unknown subtype of the given type" (where the type is Object in the case of just ? and Number in the case of ? extends Number).

Since ? indicates that you do not know what specific type of object the List will accept, it isn't legal to add anything but null to it. You are, however, allowed to retrieve any object from it, which is the purpose of using a ? extends X bounded wildcard type. Note that the opposite is true for a ? super X bounded wildcard type... a List<? super Integer> is "a list of some unknown type that is at least a supertype of Integer". While you don't know exactly what type of List it is (could be List<Integer>, List<Number>, List<Object>) you do know for sure that whatever it is, an Integer can be added to it.

Finally, new ArrayList<?>() isn't legal because when you're creating an instance of a paramterized class like ArrayList, you have to give a specific type parameter. You could really use whatever in your example (Object, Foo, it doesn't matter) since you'll never be able to add anything but null to it since you're assigning it directly to an ArrayList<?> reference.

这篇关于Java泛型 - ArrayList的初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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