java无法引用int,“int不能被解除引用”在Java中

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 is of primitive type int and not an Object. You cannot call methods on a primitive as you are doing here :

id.equals

Try replacing this:

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

with

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值