2016-04-02 17 views
0

練習では、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型が必要です。

あなたはその理由を知っていますか?私は何をすべきか?感謝! ありがとう!

+1

'反復処理可能' 'に変更Iterable'を。 –

+0

ありがとうございます! –

答えて

0

Iterableはjavaの汎用インターフェースです。つまり、型に対してパラメータ化され、基になるコレクション内のオブジェクトの型を識別するために特定の型パラメータが必要です。型パラメータを省略すると、単純にIterable<Object>になります。

、明示的にキャストすることなく、あなたのコレクションにStringsで動作するようにあなたのBucket宣言を変更できるように:それは文字列の反復を期待して

public class Bucket implements Iterable<String> { 
... 
} 
関連する問題