問題描述
我對 Java 還很陌生,我正在使用 BlueJ.我在嘗試編譯時不斷收到這個Int cannot be dereferenced"錯誤,我不確定問題是什么.該錯誤特別發(fā)生在我底部的 if 語句中,它說等于"是一個錯誤,int 不能被取消引用".希望得到一些幫助,因為我不知道該怎么做.提前謝謝!
I'm fairly new to Java and I'm using BlueJ. I keep getting this "Int cannot be dereferenced" error when trying to compile and I'm not sure what the problem is. The error is specifically happening in my if statement at the bottom, where it says "equals" is an error and "int cannot be dereferenced." Hope to get some assistance as I have no idea what to do. Thank you in advance!
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
is of primitive type int
and not an Object
. You cannot call methods on a primitive as you are doing here :
id.equals
嘗試替換這個:
if (id.equals(list[pos].getItemNumber())){ //Getting error on "equals"
與
if (id == list[pos].getItemNumber()){ //Getting error on "equals"
這篇關于“int 不能被取消引用";在爪哇的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!