读书人

麻烦大家帮小弟我看下HashSet用法的小

发布时间: 2012-01-02 22:40:04 作者: rapoo

麻烦大家帮我看下HashSet用法的小问题!
我的目地是有一个字符数组,我要通过Set接口过滤掉相同的元素,可是不知道怎么写了
import java.util.*;
class Test{
public static void main(String[] args){
String[] a=new String[]{ "a ", "b ", "c ", "c "};
HashSet hs=new HashSet(a);

Iterator it=hs.iterator();
while(it.hasNext()){
Object o=it.next();
System.out.print(o.hashCode()+ " ");
System.out.println(o);
}



}

}

[解决办法]
import java.util.*;
class Test{
public static void main(String[] args){
String[] a=new String[]{ "a ", "b ", "c ", "c "};
HashSet hs=new HashSet();
for(int i=0;i <a.length;i++)
hs.add(a[i]);

Iterator it=hs.iterator();
while(it.hasNext()){
Object o=it.next();
System.out.print(o.hashCode()+ " ");
System.out.println(o);
}



}

}
没试.不过应该没有问题了

读书人网 >J2SE开发

热点推荐