Java中的“int不能被取消引用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19109131/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:10:55  来源:igfitidea点击:

"int cannot be dereferenced" in Java

javaintbluej

提问by BBladem83

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!

我对 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();
            }
        }
    }
}

采纳答案by Juned Ahsan

idis of primitive type intand not an Object. You cannot call methods on a primitive as you are doing here :

id是原始类型int而不是Object. 你不能像你在这里做的那样在原语上调用方法:

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"

回答by John3136

Assuming getItemNumber()returns an int, replace

假设getItemNumber()返回一个int,替换

if (id.equals(list[pos].getItemNumber()))

if (id.equals(list[pos].getItemNumber()))

with

if (id == list[pos].getItemNumber())

if (id == list[pos].getItemNumber())

回答by MadProgrammer

Basically, you're trying to use intas if it was an Object, which it isn't (well...it's complicated)

基本上,你试图使用int它,好像它是一个Object,它不是(嗯......它很复杂)

id.equals(list[pos].getItemNumber())

Should be...

应该...

id == list[pos].getItemNumber()

回答by Code-Apprentice

Change

改变

id.equals(list[pos].getItemNumber())

to

id == list[pos].getItemNumber()

For more details, you should learn the difference between the primitive types like int, char, and doubleand reference types.

有关更多详细信息,您应该了解原始类型(如intchar、 和double和 引用类型)之间的区别。

回答by Ali Hashemi

try

尝试

id == list[pos].getItemNumber()

instead of

代替

id.equals(list[pos].getItemNumber()

回答by Ashit

As your methods an int datatype, you should use "==" instead of equals()

作为 int 数据类型的方法,您应该使用“==”而不是 equals()

try replacing this if (id.equals(list[pos].getItemNumber()))

尝试替换这个 if (id.equals(list[pos].getItemNumber()))

with

if (id.equals==list[pos].getItemNumber())

it will fix the error .

它将修复错误。