gpt4 book ai didi

java - Java中的"int cannot be dereferenced"

转载 作者:IT老高 更新时间:2023-10-28 20:53:24 25 4
gpt4 key购买 nike

我对 Java 还很陌生,我正在使用 BlueJ。我在尝试编译时不断收到这个“Int cannot be dereferenced”错误,我不确定问题是什么。该错误特别发生在我底部的 if 语句中,它说“等于”是一个错误,“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();
}
}
}
}

最佳答案

id 是原始类型 int 而不是 Object。您不能像在此处那样调用原语上的方法:

id.equals

尝试替换这个:

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

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

关于java - Java中的"int cannot be dereferenced",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19109131/

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