quot;int cannot be dereferencedquot; in Java(“int 不能被取消引用;在爪哇)
问题描述
我对 Java 还很陌生,我正在使用 BlueJ.我在尝试编译时不断收到这个Int cannot be dereferenced"错误,我不确定问题是什么.该错误特别发生在我底部的 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 不能被取消引用";在爪哇的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:“int 不能被取消引用";在爪哇
基础教程推荐
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 降序排序:Java Map 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01