有一数组,如int[] a={2,3,4,3,5,15,44,33,66,3,7,7,9};将数组排序,并输出如下
2 出现次数为:1 次
3 出现次数为:3 次
4 出现次数为:1 次
5 出现次数为:1 次
7 出现次数为:2 次

解决方案 »

  1.   

    http://www.java2000.net/p9635参考这个,
      

  2.   

    import java.util.*;public class Statistics{
    public static void main(String args[]) {
    int[] a = {2,3,4,3,5,15,44,33,66,3,7,7,9};
            Arrays.sort(a);
    Map<Integer, Integer> m = new TreeMap<Integer, Integer>();
    for(int i = 0; i < a.length; i++) {
    int item = a[i];
    Integer freq = m.get(item);
    m.put(item, freq == null ? 1: freq + 1);
    }
    Print(m);
    } static void Print(Map m) {
    Iterator it = m.entrySet().iterator();
    while(it.hasNext()) {
    Map.Entry e = (Map.Entry)it.next();
    System.out.println(e.getKey() + "出现次数为:::" + e.getValue() + "次");
    }
    }
    }
      

  3.   


    import java.util.HashMap;
    import java.util.Map;public class Test { public static void main(String[] args) {
    int[] a={2,3,4,3,5,15,44,33,66,3,7,7,9};
    Map<Integer, Integer> m = new HashMap<Integer, Integer>();
    for(int i = 0; i < a.length; i++){
    if(!m.containsKey(a[i])){
    m.put(a[i], 1);
    }else{
    m.put(a[i], m.get(a[i])+1);
    }
    }
    }
    }
      

  4.   


    import java.util.HashMap;
    import java.util.Map;public class Test {    public static void main(String[] args) {
            int[] a={2,3,4,3,5,15,44,33,66,3,7,7,9};
            Map<Integer, Integer> m = new HashMap<Integer, Integer>();
            for(int i = 0; i < a.length; i++){
                if(!m.containsKey(a[i])){
                    m.put(a[i], 1);
                }else{
                    m.put(a[i], m.get(a[i])+1);
                }
            }
        }
    }
      

  5.   

    这种题不需要用到Map吧
    public static void main(String args[]) {
    int[] a = { 2, 3, 4, 3, 5, 15, 44, 33, 66, 3, 7, 7, 9 };
    // sort
    Arrays.sort(a);
    // out
    int currentValue = a[0], count = 0;
    for (int i : a) {
    if (i == currentValue) {
    count++;
    } else {
    System.out.printf("%-3d出现次数为:%-3d\n", currentValue, count);
    currentValue = i;
    count = 1;
    }
    }
    }
      

  6.   

    单单从本题来讲
    用HashMap从哪方面讲都没感觉到好处