Java去重

1.数组去重:

private static String[] quChong(String[] arr) {
    ArrayList<String> list  = new ArrayList<String>();
    for (String str : arr) {
        if(!list.contains(str))
            list.add(str);
    }
    String[] newArr = list.toArray(new String[list.size()]);
    return newArr;
}

2.ArrayList去重:

private static ArrayList<String> quChong(ArrayList<String> list) {
    ArrayList<String> newList  = new ArrayList<String>();
    for (String str : list) {
        if(!newList.contains(str))
            newList.add(str);
    }
    return newList;
}
private static ArrayList<String> quChong(ArrayList<String> list){ Set<String> set = new HashSet<String>(arrayList);
//若关心元素在ArrayList中的顺序,可以用LinkedHashSet list.clear(); list.addAll(set); return list; }

 

posted @ 2017-03-09 19:56  如果屈原会编程  阅读(3293)  评论(0编辑  收藏  举报