“警告:[未经检查]未经检查的演员表"将 Object 转换为 ArrayList<String[]> 时 [英] "Warning: [unchecked] unchecked cast" when casting Object to ArrayList<String[]>
问题描述
奇怪的情况 - 下面是代码:
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:unchecked
),我收到一个警告:
When project is built (with compiler option -Xlint:unchecked
in project properties), I get one warning:
警告:[未选中]未选中的演员
ArrayList list = (ArrayList) obj[1];
需要:ArrayList
找到:对象
warning: [unchecked] unchecked cast
ArrayList list = (ArrayList) obj[1];
required: ArrayList
found: Object
但是以同样的方式转换 String 是可以的.这里有什么问题?
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
应该是ArrayList>list = (ArrayList>) obj[1];
这篇关于“警告:[未经检查]未经检查的演员表"将 Object 转换为 ArrayList<String[]> 时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!