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去重:spa

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; }
相關文章
相關標籤/搜索