35

私は Java の初心者で、BlueJ を使用しています。コンパイルしようとすると、「Int を逆参照できません」というエラーが表示され続けますが、何が問題なのかわかりません。エラーは、「equals」がエラーであり、「int を逆参照できない」と表示されている一番下の if ステートメントで具体的に発生しています。どうすればいいのか分からないので、何か参考になれば幸いです。前もって感謝します!

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

7 に答える 7

29

idはプリミティブ型intであり、 ではありませんObject。ここで行っているように、プリミティブでメソッドを呼び出すことはできません:

id.equals

これを置き換えてみてください:

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

        if (id == list[pos].getItemNumber()){ //Getting error on "equals"
于 2013-10-01T06:10:45.213 に答える
6

int基本的に、それが であるかのように使用しようとしていますが、そうObjectではありません (まあ...複雑です)

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

する必要があります...

id == list[pos].getItemNumber()
于 2013-10-01T06:10:59.913 に答える
0

getItemNumber()を返すと仮定するとint、置換

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

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

于 2013-10-01T06:10:47.927 に答える
0

変化する

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

id == list[pos].getItemNumber()

詳細については、 、 、および参照型などのプリミティブ型の違いを学ぶ必要がintありcharますdouble

于 2013-10-01T06:11:02.230 に答える
0

逆参照は、参照によって参照される値にアクセスするプロセスです。int はすでに値 (参照ではない) であるため、逆参照することはできません。したがって、コード (.) を (==) に置き換える必要があります。

于 2022-02-21T05:20:34.707 に答える
-1

試す

id == list[pos].getItemNumber()

それ以外の

id.equals(list[pos].getItemNumber()
于 2013-10-01T06:12:46.903 に答える