練習では、javaのstring-LinkedList []型のリンクリストの配列を作成するように求められました。これを行うために、BucketというLinkedListのプロパティを持つラッパークラスを作成し、代わりにバケットの配列を作成しました。これは、バケットクラスです:クラスイテレータで互換性のないタイプ
import java.util.Iterator;
import java.util.LinkedList;
/**
* A class representing a bucket in an open hash set of strings.
* It's a wrapper-class that has a LinkedList<String> that delegates methods to it.
*/
public class Bucket implements Iterable{
private LinkedList<String> listOfStrings = new LinkedList<>();
//Methods
/**
* @param searchValue a string to check.
* @return true if the property listOfString contains searchValue false otherwise.
*/
public boolean contains (String searchValue) {
return listOfStrings.contains(searchValue);
}
/**
* @param newValue A string to add to the property listOfString.
* @return true if newValue wasn't in the list and was added successfully, false other wise.
*/
public boolean add (String newValue){
if (contains(newValue)){
return false;
} else {
listOfStrings.add(newValue);
return true;
}
}
/**
* @param toDelete a string to delete from the property listOfString.
* @return true if the String was in the property listOfString and was deleted successfully false
* otherwise.
*/
public boolean delete (String toDelete){
if (contains(toDelete)){
listOfStrings.remove(toDelete);
return true;
} else{
return false;
}
}
@Override
public Iterator<String> iterator(){
return listOfStrings.iterator();
}
}
のLinkedListからバケットクラスの委譲メソッドので、私は、リンクリストの配列としてそれを使用できるようになります。私はそれを反復処理しようとすると、問題が開始されます。互換性のない型:エラーがありますする第二中
simpleHashArray = new Bucket[newTableSize];//an array of Buckets
for (Bucket bucketToCheck : simpleHashArray){
for(String stringToAdd : bucketToCheck){
add(stringToAdd);
}
}
。何らかの理由でString型の代わりにObject型が必要です。
あなたはその理由を知っていますか?私は何をすべきか?感謝! ありがとう!
'反復処理可能' 'に変更Iterable'を。 –
ありがとうございます! –