2011-12-01 85 views
0

我想从一个集合中删除长度为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 

可以请你帮帮我?

+0

而不是删除旧的代码,翻了你有INT无前提的推断的有用的答案的,我认为它会更好,在你的问题的末尾添加新的代码,或者甚至要求新题。 –

回答

2
Iterator<String> it= newStr.iterator(); 
while(it.hasNext()) { // iterate 
    String word = it.next(); 
    if(word.length() == 5) { // predicate 
     it.remove(); // remove from set through iterator - action 
    } 
} 
+0

你能解释为什么String [] setStr = set1.toArray(new String [0]);需要吗?为什么我不能迭代一个集合?你的方法有效。 –

+0

为什么要创建一个新的String []?为什么不使用迭代器? – mprabhat

+0

@MacosxIam:避免并发修改异常。但你可以直接从迭代器实例中删除元素 –

0

字符串是immutable,为String word或其他任何字符串所做的更改不会的Set

字符串中反映添加

if(word.length()==5) 
    { 
     word.replaceAll(word, ""); 
     newSet.remove(word); 
    } 

你可以参考HashSet的

remove(Object o) 
的这个功能

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/HashSet.html

+0

那么我该如何修改字符串呢? –

+0

你可以从Set中删除元素,请参阅已编辑的答案@MacosxIam – Zohaib

+0

这给了我一个错误 - 线程“main”中的异常java.util.ConcurrentModificationException –

0

字符串在Java中是不可变的,这意味着当您调用word.replaceAll(word,"")时,它将返回字符串“”(您不分配给任何东西)。 不会更改,并且该集仍然指向旧值。您需要从Set中删除

+0

我试过这个:newSet.remove(word);但它给了我一个错误 - java.util.ConcurrentModificationException –

+0

当你迭代它时你正在从Set中移除。您可以尝试创建一个新的Set,*添加不是**的**的字符串,然后分配'newSet =(您刚刚创建的用于保存非长度为5的字符串的集合)' –

+0

或者约翰·韦斯利王子建议,通过迭代Set的数组而不是Set本身。 –

0

当你发现长度为5的字符串,你需要从组中删除:

newSet.remove(word); 

正因为如此,你似乎是试图改变字为空字符串,但字符串是不可变的。你的调用实际上做的是返回一个空字符串。

2

或其他建议你不能改变一个字符串原因是,代码片断:

import java.util.HashSet; 
import java.util.Iterator; 
import java.util.Set; 

public class TestString { 
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 (Iterator<String> iter = newSet.iterator(); iter.hasNext();) { 
     if (iter.next().length() == 5) { 
      iter.remove(); 
     } 
    } 
    System.out.println(newSet); 
} 

public static void main(String[] args) { 
    new TestString().remove5(); 
} 
} 

如果您遍历集合,并在循环中,您删除的对象,它会抛出你ConcurrentModificationExceptionHastSet迭代器是一个失败的快速迭代器。

+0

这是值得解释的原因,其他答案导致['ConcurrentModificationException'](http://docs.oracle.com/javase/6/docs/api/java/util/ConcurrentModificationException.html)。 –

+0

更新我的答案,以迎合它。 – mprabhat

2

对于实际修改你的设置,你需要做这样的事情:

Iterator<String> iter = newSet.iterator(); 
while (iter.hasNext()) 
    if (iter.next().length() == 5) 
     iter.remove(); 

由于字符串是不可变的,你不能修改已经添加到集合了,反正的,即使你可以在原地修改它们,用“”代替它们不会将它们从集合中删除。

+0

+1。正是我要做的。 –

0
int i = 0; 
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(i); 
    } 
    i++; 
}