0
私はuniqueStringsがリストのjava:リストにオブジェクトを作成するには?
List<String> uniqueStrings = stringSet.getElements();
CompSet<String> stringSet2 = new CompSet<>(uniqueStrings);
assert stringSet.equals(stringSet2);
は "A"、 "B"、 "C" が含まれています。
クラスCompSet
では、関数に渡されるオブジェクトの内容がCompSet
の内容と同じかどうかを確認するequals関数があります。私は、コードを実行するとき
public List<T> getElements() {
List<T> ele = new ArrayList();
for (int i =0;i<NUBMER_OF_BUCKETS;i++){
ele.addAll(storage[i]);
}
return ele;
}
public boolean equals(Object other){
List<T> x = (List)other;
return x.equals(this.getElements());
}
はまだ、次のエラーが発生します。
とjava.lang.ClassCastException:hk.edu.polyu.comp.comp2021.assignment4.CompSetは、Javaにキャストすることはできません。 util.List
これに対処する考えはありますか?
CompSetのクラス定義を表示 –
なぜあなたはリストにキャストしていますか? –
これは意味がありません。 'this.equals(other)'は 'this.getElements()。equals(other)'と同じであってはいけません。 – shmosel