Java中的“无法取消引用”
我是Java的新手,正在使用BlueJ。尝试编译时,我不断收到此“无法取消引用Int”错误,但我不确定是什么问题。该错误专门在底部的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中的“无法取消引用” 的全部内容, 来源链接: utcz.com/qa/436022.html