私はプログラムを書いていますが、特定のオブジェクトがリストにあるかどうかを確認してから呼び出す必要があります。クラスに実装したインターフェースのメソッドcontains()
を使用するはずのメソッドをセットアップしました が、それを呼び出していないようです(チェックするためにprintステートメントを入れました)。コードの何が問題なのかわかりません。リストを調べるために使用しているクラスは、クラスで定義した正しいメソッドを使用していますが、何らかの理由で実装したメソッドを使用しません。equals()
Comparable
Golfer
ArrayUnsortedList
toString()
Golfer
equals()
//From "GolfApp.java"
public class GolfApp{
ListInterface <Golfer>golfers = new ArraySortedList<Golfer> (20);
Golfer golfer;
//..*snip*..
if(this.golfers.contains(new Golfer(name,score)))
System.out.println("The list already contains this golfer");
else{
this.golfers.add(this.golfer = new Golfer(name,score));
System.out.println("This golfer is already on the list");
}
//From "ArrayUnsortedList.java"
protected void find(T target){
location = 0;
found = false;
while (location < numElements){
if (list[location].equals(target)) //Where I think the problem is
{
found = true;
return;
}
else
location++;
}
}
public boolean contains(T element){
find(element);
return found;
}
//From "Golfer.java"
public class Golfer implements Comparable<Golfer>{
//..irrelavant code sniped..//
public boolean equals(Golfer golfer)
{
String thisString = score + ":" + name;
String otherString = golfer.getScore() + ":" + golfer.getName() ;
System.out.println("Golfer.equals() has bee called");
return thisString.equalsIgnoreCase(otherString);
}
public String toString()
{
return (score + ":" + name);
}
私の主な問題は、 の find 関数を取得しArrayUnsortedList
て equals 関数を呼び出すことのfind()
ようList
ですが、印刷したときに言ったように、toString()
完全に実装したメソッドで動作する理由は正確にはわかりません。
問題は、メソッドを呼び出さないfind()
関数に関係しているとほぼ確信しています。メソッドに依存する他の関数を使用してみましたが、同じ結果が得られました。ArraySortedList
equals()
find()