&QUOT;警告:[选中]选中投&QUOT;铸造对象的ArrayList&LT时;的String []&GT; [英] "Warning: [unchecked] unchecked cast" when casting Object to ArrayList<String[]>
问题描述
怪现状 - 下面是code:
Strange situation - below is the code:
ArrayList<String[]> listArr = new ArrayList<>();
Object[] obj = new Object[]{"str", listArr};
String str = (String) obj[0];//OK
ArrayList<String[]> list = (ArrayList<String[]>) obj[1];//warning: [unchecked] unchecked cast
在项目建成(与编译器选项 -Xlint:选中
项目属性),我得到一个警告:
When project is built (with compiler option -Xlint:unchecked
in project properties), I get one warning:
警告:未选中]选中投结果
ArrayList的列表=(ArrayList的)目标文件[1];结果要求:ArrayList的结果发现:
对象
warning: [unchecked] unchecked cast
ArrayList list = (ArrayList) obj[1];
required: ArrayList
found: Object
但是,在以相同的方式浇铸字符串是确定。有什么问题吗?
But casting String in the same way is OK. What is the problem here?
推荐答案
这是因为编译器不能在列表级别验证的内部类型,所以你需要先验证列表。和内部类型分别。
This is because the compiler can not verify the internal types at the list level, so you need to first verify for list. And the internal types individually.
而不是的ArrayList&LT的;的String []&GT;名单=(ArrayList的&LT;的String []&GT;)的obj [1];
它应该是的ArrayList&LT;&GT;名单=(ArrayList的&LT;&GT;)的obj [1];
这篇关于&QUOT;警告:[选中]选中投&QUOT;铸造对象的ArrayList&LT时;的String []&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!