我有两个数组String []arr={"1","2","3"}
String []arr1={"1","2","3","4","5"},谁能帮我把数组arr1中与arr中不同的元素给我提出来放到另一个数组中,也就是把4.5.给提出来,谢谢,希望大哥大姐们,来帮帮忙,

解决方案 »

  1.   

    public class TheDifferentElementsInArray {   public TheDifferentElementsInArray() {        super();
        }    public List getDifferent(Object[] oneArray, Object[] anotherArray) {        if (oneArray == null || oneArray.length == 0 || anotherArray == null
                || anotherArray.length == 0) {
                return new ArrayList(0);
            }        Object[] maxArray = oneArray;
            Object[] minArray = anotherArray;
            int maxSize = maxArray.length;
            if (maxArray.length < minArray.length) {
                maxSize = minArray.length;
                maxArray = anotherArray;
                minArray = oneArray;
            }        List minList = Arrays.asList(minArray);
            List differents = new ArrayList();
            for (int i = 0; i < maxSize; i++) {
                if (minList.contains(maxArray[i])) {
                    continue;
                }            differents.add(maxArray[i]);
            }        return differents;
        }    public static void main(String[] args) {        TheDifferentElementsInArray test = new TheDifferentElementsInArray();        String[] a = {"1", "2", "3"};
            String[] b = {"1", "2", "4", "5", "3"};        List differents = test.getDifferent(a, b);
            for (int i = 0; i < differents.size(); i++) {
                System.out.println(differents.get(i));
            }    }
    }
      

  2.   

    String []arr={"1","2","3"};
    String []arr1={"1","2","3","4","5"};
    String param = "";
    List result = new ArrayList();
    for(int i=0;i<arr.length;i++){
    param = param + "%s";
    if(i<arr.length-1) param = param + ",";
    }
    param = String.format(param,arr);
    for(int i=0;i<arr1.length;i++)
    if(param.indexOf(arr1[i]) == -1)
    result.add(arr1[i]);
    System.out.println(result);
      

  3.   

    String[] aa = {"2", "3", "4", "6", "8", "0", "41"};
    String[] bb = {"2", "3", "5", "7", "9", "0", "411"};
    Set s1 = new HashSet(Arrays.asList(aa));
    Set s2 = new HashSet(Arrays.asList(bb));
    s1.removeAll(s2);
    s2.removeAll(s1);
    s1.addAll(s2);
    System.out.println(s1);