Java泛型新的原始类型与泛型类型 [英] Java Generics new raw type vs generic type

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

问题描述

  ArrayList< String> 

我读了很多文章,但是无法理解这两行之间的区别。 list = new ArrayList();
ArrayList< String> list = new ArrayList< String>();

唯一的区别是我看到第一行触发了Unchecked assignment警告。

解决方案

您可以创建一个原始列表。

  List list = new ArrayList(); 

这很好,你现在有一个原始列表,你可以放入任何对象。如果你想把这个列表分配给一个打字版本,你将会做一个未经检查的任务。

  List< String> strings = list; 

这是因为,放入列表的内容从未被检查为字符串。


I read so many articles but I can't understand the difference between these two lines:

ArrayList<String> list = new ArrayList();
ArrayList<String> list = new ArrayList<String>();

The only difference I see that the first line trigger an "Unchecked assignment" warning.

解决方案

You could make a raw list.

List list = new ArrayList();

That is fine, you now have a raw list, that you can put any object into. If you want to assign that list to a typed version, you will make an unchecked assignment.

List<String> strings = list;

That is because, what was put into list was never checked to be a string.

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

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