私はこのファサードを実装して、LinkedList、TreeSetおよびHashSetクラスをjavaでラップします。含まれている機能でファサードの実装におけるConcurrentModificationException
import java.util.Iterator;
public class CollectionFacadeSet implements SimpleSet{
protected java.util.Collection<java.lang.String> collection;
private Iterator<java.lang.String> iterator;
private int count;
/**
* Creates a new facade wrapping the specified collection.
* @param collection - The Collection to wrap.
*/
public CollectionFacadeSet(java.util.Collection<java.lang.String> collection){
this.collection=collection;
iterator = this.collection.iterator();
count=0;
}
/**
* Add a specified element to the set if it's not already in it.
* @param newValue New value to add to the set
* @return False iff newValue already exists in the set
*/
public boolean add(java.lang.String newValue){
if(contains(newValue))
return false;
collection.add(newValue);
return true;
}
/**
* Look for a specified value in the set.
* @param searchVal Value to search for
* @return True iff searchVal is found in the set
*/
public boolean contains(java.lang.String searchVal){
while(iterator.hasNext())
{
java.lang.String myString=iterator.next(); //issue
System.out.println(myString);
if(myString.equals(searchVal))
return true;
}
return false;
}
、私は次の(現在の)オブジェクトをホストするための文字列を作成していたら、私は次のエラーを取得:
Exception in thread "main" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:966)
at java.util.LinkedList$ListItr.next(LinkedList.java:888)`
を私はかなり道のを追ってきました他の質問で書かれているが、私のループはまだ例外をスローするようだ。
あなたが名前を完全に修飾されているのはなぜ? 'java.lang.String'は必要ではありません:単に' String'を使います。 'java.util.Collection;をインポートする必要がありますが、' java.util.Collection'の代わりに 'Collection'だけ書くことができます。 –
これは私たちの大学のスタッフが望む方法です...彼らはまた、3日前までイテレータなしでコレクションを繰り返すように求めました:\ – tamir