简体   繁体   English

什么是未经检查和不安全的操作?

[英]What is unchecked and unsafe operation here?

I have the following code: 我有以下代码:

private static final Set<String> allowedParameters;
static {
    Set<String> tmpSet = new HashSet();
    tmpSet.add("aaa");
    allowedParameters = Collections.unmodifiableSet(tmpSet);
}

And it cause: 它导致:

Note: mygame/Game.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

And when I recompile with the suggested option I see a pointer (^) pointing at "new" in front of HashSet(); 当我使用建议的选项重新编译时,我看到一个指针(^)指向HashSet();前面的“new” HashSet(); .

Does anybody know what is going on here? 有谁知道这里发生了什么?

Yes, you're creating a new HashSet without stating what class it should contain, and then asserting that it contains strings. 是的,你正在创建一个新的HashSet而不说明它应该包含哪个类,然后断言它包含字符串。 Change it to 将其更改为

 Set<String> tmpSet = new HashSet<String>();

these messages occur when you are using classes that support the new J2SE 1.5 feature - generics. 当您使用支持新J2SE 1.5功能的类 - 泛型时,会出现这些消息。 You get them when you do not explicitly specify the type of the collection's content. 如果没有明确指定集合内容的类型,则可以获取它们。

For example: 例如:

List l = new ArrayList();
list.add("String");
list.add(55);

If you want to have a collection of a single data type you can get rid of the messages by: 如果您想拥有单一数据类型的集合,可以通过以下方式删除消息:

List<String> l = new ArrayList<String>();
list.add("String");

If you need to put multiple data types into once collection, you do: 如果您需要将多种数据类型放入一次集合中,您可以:

List<Object> l = new ArrayList<Object>();
list.add("String");
list.add(55);

If you add the -Xlint:unchecked parameter to the compiler, you get the specific details about the problem. 如果将-Xlint:unchecked参数添加到编译器,则会获得有关该问题的特定详细信息。

for more details refer here : http://forums.sun.com/thread.jspa?threadID=584311 有关详细信息,请参阅此处: http//forums.sun.com/thread.jspa?threadID = 584311

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM