日期:2014-05-17 浏览次数:20769 次
String[] old = { "类目一", "类目二", "类目三", "类目一", "类目三" }; List<String> list = new ArrayList<String>(); for (String str : old) { if (!list.contains(str)) { list.add(str); } } String[] dest = new String[list.size()]; list.toArray(dest); for (String str : dest) { System.out.println(str); }
------解决方案--------------------
1楼可以,另外用map也可以,不用判断。
------解决方案--------------------
自己看着需要修改。。。
public static void main(String[] args) throws Exception { String[] old = { "类目一", "类目二", "类目三", "类目一", "类目三" }; Map<String, Integer> map = new TreeMap<String, Integer>(); for (String key : old) { if (map.containsKey(key)) { map.put(key, map.get(key) + 1); } else { map.put(key, 1); } } String[] des = new String[map.size()]; int i = 0; for (String key : map.keySet()) { des[i++] = key + " : " + map.get(key); } System.out.println(Arrays.toString(des)); }
------解决方案--------------------
public static String[] replace(String[] array, Map<String, Integer> map) { if (array != null && array.length > 0) { int len = array.length, v = 0; for (int i = 0; i < len; i++) { if (map.containsKey(array[i])) { v = map.get(array[i]); map.put(array[i], v + 1); } else map.put(array[i], 1); } // System.out.println(map.entrySet()); return map.keySet().toArray(new String[map.keySet().size()]); } return array; } public static void main(String[] args) { String[] old = new String[] { "类目一", "类目二", "类目三", "类目一", "类目三" }; Map<String, Integer> map = new HashMap<String, Integer>(); print(replace(old, map)); System.out.println(map); // print(old); }
------解决方案--------------------
String[] old = { "类目一", "类目二", "类目三", "类目一", "类目三" }; Map<String,Integer> map = new HashMap<String,Integer>(); for(int i =0;i<old.length;i++){ if(!map.containsKey(old[i])) map.put(old[i], 1); else map.put(old[i], map.get(old[i]+1)); }