mb-*_*xas 4 java arraylist unchecked
我想要一个列表,然后我想测试列表以查看项目是否存在:
这是我的示例代码段:
String[] handToolArray = {"pliers", "screwdriver", "tape measure"};
List<String> handToolList = new ArrayList<String>( Arrays.asList(handToolArray));
if (handToolList.contains("pliers")){
System.out.println("I have pliers");
} else {
System.out.println("I don't have pliers");
}
Run Code Online (Sandbox Code Playgroud)
在第二行中,Arrays.asList(handToolArray)生成:
"Type safety: The expression of type List needs unchecked conversion to conform to Collection<? extends String>"
Run Code Online (Sandbox Code Playgroud)
问题:有没有更好的方法来创建然后查询列表,这是简洁的,不需要抑制未经检查的警告?
您可以在不显式构造数组的情况下执行此操作(数组仍由varargs使用).
List<String> handToolList = Arrays.asList("pliers", "screwdriver", "tape measure");
Run Code Online (Sandbox Code Playgroud)