任意两个字符数组,比较两个字符数组中相同的元素放到一个数组中,不相同的元素放到另外一个数组中。
输出求得的两个数组。

解决方案 »

  1.   

    提供个思路 创建2个StringBuffer 一个叫same  一个叫diff  然后相同的字符就放到same中去 不同的就放到diff中去 最后输出比较相同还是不同  没有什么办法 遍历比较 或者先排序 再比较 。感觉排序比较的算法要简洁点 因为字符是有顺序的 当数组1中的a 和数组2中的第一个元素比较 (如果是b)那么就可以跳出循环了 因为以为后面不可能再出现a了  
      

  2.   

    public static void main(String[] args) {
    String[] array1 = {"1", "2", "3", "4", "5"};
    String[] array2 = {"4", "5", "6", "7"};

    List<String> list1 = new LinkedList<String>();
    List<String> list2 = new LinkedList<String>();
    List<String> tmp1 = new LinkedList<String>();
    for(String s : array1) {
    list1.add(s);
    tmp1.add(s);
    }
    for(String s : array2) {
    list2.add(s);
    }

    tmp1.retainAll(list2);
    String[] same = new String[tmp1.size()];
    tmp1.toArray(same);

    System.out.println("Same value set is:");
    System.out.print("[");
    for(String s : same) {
    System.out.print(s + ",");
    }
    System.out.println("]");

    list1.removeAll(tmp1);
    list2.removeAll(tmp1);
    list1.addAll(list2);
    String[] diff = new String[list1.size()];
    list1.toArray(diff);

    System.out.println("Diff value set is:");
    System.out.print("[");
    for(String s : diff) {
    System.out.print(s + ",");
    }
    System.out.println("]");
    }
      

  3.   

    public static void main(String[] args) {
    String[] array1 = {"1", "2", "3", "4", "5"};
    String[] array2 = {"4", "5", "6", "7"};

    List<String> list1 = new LinkedList<String>();
    List<String> list2 = new LinkedList<String>();
    List<String> tmp1 = new LinkedList<String>();
    for(String s : array1) {
    list1.add(s);
    tmp1.add(s);
    }
    for(String s : array2) {
    list2.add(s);
    }

    tmp1.retainAll(list2);
    String[] same = new String[tmp1.size()];
    tmp1.toArray(same);

    System.out.println("Same value set is:");
    System.out.print("[");
    for(String s : same) {
    System.out.print(s + ",");
    }
    System.out.println("]");

    list1.removeAll(tmp1);
    list2.removeAll(tmp1);
    list1.addAll(list2);
    String[] diff = new String[list1.size()];
    list1.toArray(diff);

    System.out.println("Diff value set is:");
    System.out.print("[");
    for(String s : diff) {
    System.out.print(s + ",");
    }
    System.out.println("]");
    }第一次没了格式...