gpt4 book ai didi

java - 使用未经检查或不安全的操作,使用 -Xlint 重新编译

转载 作者:行者123 更新时间:2023-12-03 13:04:07 28 4
gpt4 key购买 nike

我已经处理这个问题一个星期了,虽然我找到了一些答案,但似乎没有一个能解决我程序的问题。

我不断收到这条消息:

Note: Triangle.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

代码如下:

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

感谢任何帮助,谢谢!

最佳答案

问题是您没有为 ArrayList 指定类型,因此没有编译时检查您是否正在插入,例如,将整数插入 String ArrayList。由于这将是“不安全的操作”,您会收到警告。

要修复,替换 private ArrayList btsarr;private ArrayList<String> btsarr;this.btsarr = new ArrayList();this.btsarr = new ArrayList<String>(); .

通常,在实例化 ArrayList 和 HashMap(或任何其他 Java 集合)时,您应该始终指定您想要保存的对象类型。

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

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