Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java &引用;int不能被取消引用";在爪哇_Java_Int_Bluej - Fatal编程技术网

Java &引用;int不能被取消引用";在爪哇

Java &引用;int不能被取消引用";在爪哇,java,int,bluej,Java,Int,Bluej,我对Java相当陌生,我正在使用BlueJ。在尝试编译时,我一直遇到这个“Int不能被取消引用”错误,我不确定问题出在哪里。错误具体发生在我的if语句底部,它说“equals”是一个错误,“int不能被解引用”。希望得到一些帮助,因为我不知道该怎么做。提前谢谢你 public class Catalog { private Item[] list; private int size; // Construct an empty catalog with the spec

我对Java相当陌生,我正在使用BlueJ。在尝试编译时,我一直遇到这个“Int不能被取消引用”错误,我不确定问题出在哪里。错误具体发生在我的if语句底部,它说“equals”是一个错误,“int不能被解引用”。希望得到一些帮助,因为我不知道该怎么做。提前谢谢你

public class Catalog {
    private Item[] list;
    private int size;

    // Construct an empty catalog with the specified capacity.
    public Catalog(int max) {
        list = new Item[max];
        size = 0;
    }

    // Insert a new item into the catalog.
    // Throw a CatalogFull exception if the catalog is full.
    public void insert(Item obj) throws CatalogFull {
        if (list.length == size) {
            throw new CatalogFull();
        }
        list[size] = obj;
        ++size;
    }

    // Search the catalog for the item whose item number
    // is the parameter id.  Return the matching object 
    // if the search succeeds.  Throw an ItemNotFound
    // exception if the search fails.
    public Item find(int id) throws ItemNotFound {
        for (int pos = 0; pos < size; ++pos){
            if (id.equals(list[pos].getItemNumber())){ //Getting error on "equals"
                return list[pos];
            }
            else {
                throw new ItemNotFound();
            }
        }
    }
}
公共类目录{
私人物品[]清单;
私有整数大小;
//构造具有指定容量的空目录。
公共目录(int max){
列表=新项目[最大值];
尺寸=0;
}
//将新项目插入目录。
//如果目录已满,则引发CatalogFull异常。
公共作废插入(项目obj)已满{
if(list.length==大小){
抛出新目录full();
}
列表[大小]=对象;
++大小;
}
//在目录中搜索其项目编号为的项目
//是参数id。返回匹配的对象
//如果搜索成功,则抛出ItemNotFound
//搜索失败时的异常。
公共项查找(int-id)抛出ItemNotFound{
用于(int pos=0;pos
id
是原始类型
int
,而不是
对象。不能像在此处所做的那样对基元调用方法:

id.equals
尝试替换此:

        if (id.equals(list[pos].getItemNumber())){ //Getting error on "equals"


假设
getItemNumber()
返回一个
int
,则替换

if(id.equals(list[pos].getItemNumber())


if(id==list[pos].getItemNumber())

基本上,您试图使用
int
就像它是一个
对象一样,而它不是(嗯……它很复杂)

应该是

id == list[pos].getItemNumber()
改变

有关更多详细信息,您应该了解基本类型(如
int
char
)和
double
与引用类型之间的区别。

请尝试

id == list[pos].getItemNumber()
而不是

id.equals(list[pos].getItemNumber()

作为int数据类型的方法,应该使用“==”而不是equals()

试着换掉这个 if(id.equals(list[pos].getItemNumber())


它将修复错误。

您尝试使用
int
时,需要使用
整数
数字
对象
int
没有任何方法毫无疑问:==比较对象的引用和比较原语的值,对吗?如果我错了,请改正。原语是特殊的。实际上,在研究界面时,我收到了这个错误,通过谷歌搜索我得到了这个答案。请查看是否可以:
错误:int不能被取消引用
System.out.println(“A=“+A.AB”)
调用SOP的类实现一个
接口C
,一个是C的超级接口。int AB在两个接口中都定义。错误发生在
A.AB
int AB
不能在任何
接口中声明,它只能在
类中声明
…如果需要使用
整数,我该怎么办?
id.equals(list[pos].getItemNumber())
id == list[pos].getItemNumber()
id == list[pos].getItemNumber()
id.equals(list[pos].getItemNumber()
if (id.equals==list[pos].getItemNumber())