一个数组,怎么把重复的值和不重复的值 分别保存在其他2个数组里。。想了好久不回啊。
求大哥们指点。
------解决方案--------------------
@Test
public void test(){
String[] strs={"a","a","b","c"};
Set<String> reset=new HashSet<String>();//重复的数组
Set<String> noreset=new HashSet<String>();//不重复的数组
for(int i=0;i<strs.length;i++){//遍历旧数组
if(!noreset.add(strs[i])){//如果存入不成功
noreset.remove(strs[i]);
reset.add(strs[i]);
}
}
String[] s1=reset.toArray(new String[reset.size()]);//重复的数组
String[] s2=reset.toArray(new String[noreset.size()]);//不重复的数组
//打印
System.out.println(s1);
System.out.println(s2);
}
改成这样吧 这样s1、s2就是最后需要的数组了 上面的那个 只是把数组打印了 其实打印流也没啥意义 打出来肯定是地址值
------解决方案--------------------
第一个元素先放进一个数组A中,后面的元素取出时都和这个数组A遍历查询一下,有重复的放另一个数组B中,结束后,A中把和B中一样的减掉
------解决方案--------------------
漏洞百出!
