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; }