比如一组数字[1,2,3,4,5,4,4,4,2,2,1,1,6],分为[1,1,1],[3],[2,2,2],[4,4,4,4],[5],[6]应该怎么分java

解决方案 »

  1.   

    List t = null;
    int[] arr = new int[] {1, 2, 3, 4, 5, 4, 4, 4, 2, 2, 1, 1, 6 };
    Map m = new HashMap();
    for (int i : arr) {
    if (m.containsKey(i)) {
    List w = (List) m.get(i);
    w.add(i);
    } else {
    t = new ArrayList();
    t.add(i);
        m.put(i, t);
    }
    }
    Iterator it = m.entrySet().iterator();
    while (it.hasNext()) {
    Map.Entry v = (Map.Entry) it.next();
    System.out.println(v.getValue());
    }