我有一个String[]acctitleIdArray 数组,里面为[1,2,3,4],还有一个List limitSubList,里面为[1,3,7,8]
,问怎么遍历出acctitleIdArray在limitSubList里的值,和不在limitSubList里的值。把这两个结果集分别放到2个字符串中,字符串中的字符用,隔开。

解决方案 »

  1.   

    使用 apache commons collections 包中的collectionUtils类的intersection方法和disjunction方法可以达到你的要求
    。此方法可以算出两个集合中的并集和非并集
      

  2.   

    大侠,我刚学java没多久,您能写出来,我看看不,今天搞了一下午没弄出来,相当郁闷。
      

  3.   

    利用集合的交并差方法就可以了List<String> list = new ArrayList<String>();
    for (String s : acctitleIdArray) {
        list.add(s);
    }
    List<String> existList = new ArrayList<String>(limitSubList).retainAll(list);
    List<String> notExistList = list.removeAll(limitSubList);StringBuilder existBuf = new StringBuilder();
    for (String s : existsList) {
        existBuf.append(s).append(",");
    }
    if (existBuf.length()>0) {existBuf.delete(existBur.length()-1, existBur.length());}StringBuilder notExistBuf = new StringBuilder();
    for (String s : notExistList) {
        notExistBuf.append(s).append(",");
    }
    if (notExistBuf.length()>0) {notExistBuf.delete(notExistBuf.length()-1, notExistBuf.length());}
    String[] result = new String[]{existBuf.toString(), notExistBuf.toString()};
    return result;