我想從一個集合中刪除長度爲5的字符串,但它不斷輸出集合本身。如何從一組中刪除長度爲5的字符串?
public void remove5()
{
Set<String> newSet = new HashSet<String>();
newSet.add("hello");
newSet.add("my");
newSet.add("name");
newSet.add("is");
newSet.add("nonsense");
for(String word: newSet)
{
if(word.length()==5)
{
newSet.remove(word); // Doesn't Help - throws an error Exception in thread "main" java.util.ConcurrentModificationException
}
}
System.out.println(newSet);
}
我所要的輸出是:
my
name
is
nonsense
(除去你好,因爲它是5個字符)
但我得到這個每次:
hello
my
name
is
nonsense
可以請你幫幫我?
而不是刪除舊的代碼,翻了你有INT無前提的推斷的有用的答案的,我認爲它會更好,在你的問題的末尾添加新的代碼,或者甚至要求新題。 –