String str[]={"a","a","b","c"};
把上面数组中重复的值a,a拿出来保存在一个数组,
不重复的值b,c保存在一个数组中。这个是题意。。

解决方案 »

  1.   

    @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=new String[reset.size()];//重复的数组
    String[] s2=new String[noreset.size()];//不重复的数组
    System.out.println(reset.toArray(s1));
    System.out.println(noreset.toArray(s2));
    }
    我想了一下  这里用set的特点可能简单点   不保证是最好的  有问题可以再交流
      

  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就是最后需要的数组了  上面的那个 只是把数组打印了   其实打印流也没啥意义  打出来肯定是地址值
      

  3.   

    第一个元素先放进一个数组A中,后面的元素取出时都和这个数组A遍历查询一下,有重复的放另一个数组B中,结束后,A中把和B中一样的减掉
      

  4.   


    如果String[] strs={"a","a","a","b","c"};在测试一下
      

  5.   


        public static void test() {
            String[] strs = {"a", "a", "a", "b", "c"};
            List copy = Arrays.asList(strs.clone());
            List reset =new ArrayList();//重复的数组
            List noreset =new ArrayList();//不重复的数组
            for (int i = 0; i < strs.length; i++) {//遍历旧数组
                copy.set(i, null);
                if (copy.contains(strs[i])) reset.add(strs[i]);
                else noreset.add(strs[i]);
                copy.set(i, strs[i]);
            }
            //打印
            for (Object a : reset) System.out.println("s1:" + a);
            for (Object a : noreset) System.out.println("s2:" + a);
        }
      

  6.   


    @Test
    public void test(){
    String[] strs={"a","a","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.contains(strs[i])){//如果存入成功
    noreset.add(strs[i]);
    }else{
    reset.add(strs[i]);
    }
    }
    String[] s1=reset.toArray(new String[reset.size()]);//重复的数组
    String[] s2=noreset.toArray(new String[noreset.size()]);//不重复的数组
    }我这样就行了  是吧   愁...