java - Java 中的 "int cannot be dereferenced"

标签 java int bluej

我对 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 - Java 中的 "int cannot be dereferenced",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61279289/

相关文章:

java - final public static int 不能用在 switch 语句中?

java - Android 上停止 for 循环的按钮

c - 如何从用户那里获取整数的特定长度?

java - 在 BlueJ 中正确导入类

java - 如何在 BlueJ "Create Object"对话框中输入 LocalDate 值

Java HashMap 类抛出 NullPointerException

Java类反射: init external class with wild card parameters

Tensorflow:如何将整数张量转换为相应的二进制张量

c - float 到字符串

java - 自动更新 Java Swing 应用程序的替代方法是什么?