整数类型未按预期运行(Java) [英] Integer type not working as expected (Java)
问题描述
我正在用Java编写第一个处理泛型和标准OOP原理的程序,但是在处理Integer类型时遇到了一些问题.
I'm writing one of my first programs dealing with generics and standard OOP principles in Java, however I am running into a bit of a problem when dealing with the Integer type.
我注意到Integer应该具有一个名为compareTo(Integer anotherInteger)的方法,以便像对待int那样进行比较.
I noticed that Integers are expected to have a method named compareTo(Integer anotherInteger) in order to compare them as you would ints.
但是,当我尝试在程序中实现此方法时,遇到了问题.
However, when I tried to implement this method in my program I ran into a problem.
错误找不到符号 符号:方法compareTo(Integer) 位置:整数类型的变量x 其中Integer是类型变量:Integer扩展BinarySearchTree中声明的对象
error cannot find symbol symbol: method compareTo(Integer) location: variable x of type Integer where Integer is a type-variable: Integer extends Object declared in BinarySearchTree
基本上,我有x.comporeTo(y),其中x& y的类型为Integer,但是未找到类型为Integer(x)的变量的compareTo方法.不确定如何解决此问题,将不胜感激.
Essentially, I have x.comporeTo(y) where x & y are of type Integer, but the compareTo method is not found for variable of type Integer (x). Not sure how to solve this, any help would be appreciated.
谢谢
我在下面提供了一个小例子来突出我的问题.我相信我正在给Integer蒙上阴影,但我不知道该如何解决或解决问题,
I've provided a small example below to highlight my problem. I believe I am shadowing Integer, but I don't know how to get around doing such, or how to fix the problem,
public class IntgS<Integer>
{
Intg<Integer> z = new Intg(3);
Intg<Integer> y = new Intg(2);
int w = (z.getX()).compareTo(y.getX());
public class Intg<Integer>
{
private Integer x;
public Intg(Integer x)
{
this.x = x;
}
public Integer getX()
{
return x;
}
}
}
给出相同的错误,表示Integer类型变量z.getX()没有方法.compareTo(Integer).
This gives the same error expressing that Integer type-variable z.getX() does not have a method .compareTo(Integer).
推荐答案
根据错误消息BinarySearchTree
,有一个名为Integer
的类型参数,它遮盖了标准类型java.lang.Integer
.
According to the error message BinarySearchTree
has a type parameter called Integer
which shadows the standard type java.lang.Integer
.
这篇关于整数类型未按预期运行(Java)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!