我想从一个集合中删除长度为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无前提的推断的有用的答案的,我认为它会更好,在你的问题的末尾添加新的代码,或者甚至要求新题。 –