Java 使用未经检查或不安全的操作,使用 -Xlint 重新编译
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19351207/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uses unchecked or unsafe operations, recompile with -Xlint
提问by user2877139
I have been dealing with this problem for a week and while I've found a few answers, it seems that none of them solve my program's issues.
我已经处理这个问题一个星期了,虽然我找到了一些答案,但似乎没有一个能解决我的程序问题。
I keep getting this message:
我不断收到此消息:
Note: Triangle.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
Here is the code:
这是代码:
import java.util.*;
public class Triangle
{
public double a;
public double b;
private double c;
private ArrayList btsarr;
private static int numMade = 0;
public Triangle()
{
this.a = 3;
this.b = 4;
this.c = 5;
this.btsarr = new ArrayList();
this.btsarr.add(this.c + ""); //adding it as a String object
Triangle.numMade++;
}
public Triangle(double x1, double x2, double x3)
{
this.a = x1; //specs say no data validation necessary on parameters
this.b = x2;
this.c = x3;
this.btsarr = new ArrayList();
this.btsarr.add(this.c + "");
Triangle.numMade++;
}
public void setC(double x)
{
if (x <= 0)
{
System.out.println("Illegal, can't set c to 0 or negative");
}
else
{
this.c = x;
this.btsarr.add(this.c + "");
}
}
public double getC()
{
return this.c;
}
public void showAllCs()
{
System.out.println(this.btsarr);
}
public boolean isRightTriangle()
{
if (this.a*this.a + this.b*this.b == this.c*this.c)
return true;
else
return false;
}
public static int getNumMade()
{
return Triangle.numMade;
}
}
Any amount of help is appreciated, Thank you!
任何数量的帮助表示赞赏,谢谢!
回答by William Gaul
The problem is that you do not specify a type for your ArrayList, so there's no compile-time checking for whether you're inserting, for example, integers into a String ArrayList. Since this would be an "unsafe operation" you get the warning.
问题是您没有为 ArrayList 指定类型,因此没有编译时检查您是否将整数插入到 String ArrayList 中。由于这将是“不安全操作”,因此您会收到警告。
To fix, replace private ArrayList btsarr;
with private ArrayList<String> btsarr;
and this.btsarr = new ArrayList();
with this.btsarr = new ArrayList<String>();
.
要修复,替换private ArrayList btsarr;
用private ArrayList<String> btsarr;
和this.btsarr = new ArrayList();
用this.btsarr = new ArrayList<String>();
。
In general you should always specify what type of objects you want things like ArrayList and HashMap (or any other Java collection) to hold when you instantiate them.
一般来说,当你实例化它们时,你应该总是指定你想要什么类型的对象,比如 ArrayList 和 HashMap(或任何其他 Java 集合)。
回答by Alen Nishabu
Those are just warnings buddy. The compilation is successful. You can run your program without any problem.
这些只是警告哥们。编译成功。您可以毫无问题地运行您的程序。
If you want to suppress such warning just use the below annotation.
如果你想抑制这样的警告,只需使用下面的注释。
@SuppressWarnings("unchecked") //above the method definition.