【问题标题】:Program uses unchecked or unsafe operations, recompile using -Xlint:unchecked for details程序使用未经检查或不安全的操作,使用 -Xlint:unchecked 重新编译以获取详细信息
【发布时间】:2015-07-09 07:13:26
【问题描述】:

我只是在尝试一个简单的代码并得到这个注释。程序编译执行成功,但是这个注释打扰了我。

Program uses unchecked or unsafe operations, recompile using -Xlint:unchecked for details
  • 这里有哪些不安全或未经检查的操作?什么是不安全或未经检查的操作?
  • -Xlint:Unchecked 有什么作用?以及如何使用?

我在编译的时候是这样用的:

 javac <filename>.java -Xlint:unchecked

结果是

[unchecked] unchecked call to add <E> as a member of raw type HashSet h.add("raj");

并且在其他行中也得到了它是什么意思?

import java.util.HashSet;
import java.util.Iterator;
public class HashSetDemo
{
    public static void main(String[] args)
    {
        HashSet h = new HashSet();
        h.add("raj");
        h.add(11);
        h.add(22.2);
        h.add('c');
        h.add(true);
        h.add(null);
        h.add(null);
        System.out.println("Duplicate:"+h.add(11));
        System.out.println(h);
        Iterator itr = h.iterator();
        while(itr.hasNext())
        {
            System.out.println(itr.next());
        }
    }
}

【问题讨论】:

标签: java


【解决方案1】:

您应该使用HashSet 的类型:

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

但是你只能将String 实例放入其中。

在您的示例中,您尝试将 StringIntegerFloatBoolean 实例添加到其中,这是没有意义的。

查看the Java Tutorial 了解泛型。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多