日期:2014-05-20  浏览次数:20753 次

麻烦大家帮我看下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);
}



}

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