gpt4 book ai didi

java - 程序使用未经检查或不安全的操作,使用-Xlint :unchecked for details重新编译

转载 作者:行者123 更新时间:2023-12-01 11:20:26 25 4
gpt4 key购买 nike

我只是尝试一个简单的代码并得到这个注释。程序已成功编译并执行,但此注释让我感到不安。

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());
}
}
}

最佳答案

您应该使用 HashSet 的类型:

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

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

在您的示例中,您尝试添加 StringIntegerFloatBoolean 实例,其中没有任何意义。

看看the Java Tutorial了解泛型。

关于java - 程序使用未经检查或不安全的操作,使用-Xlint :unchecked for details重新编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31310509/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com