数组排序的基础上按升序排列出现计数 [英] Sort array based on count of occurrences in ascending order

查看:125
本文介绍了数组排序的基础上按升序排列出现计数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何能安排基于该值的出现在java的升序数量在数组中的元素。

这是我已经试过:

  int类型的[] = {0,0,0,1,3,3,2,1,3,5,6,0};
INT B =则为a.length;
的for(int i = 0; I< B;我++){
    对于(INT J = 0; J< I; J ++){
        INT温度;
        如果(一个[j]的>一种由[i]){
            TEMP = a [i];
            一个由[i] = A [J]。
            一个研究[J] =温度;
        }
    }
}对于(INT R = 0;为r则为a.length; R ++){
    的System.out.println(A [R]);
}


解决方案

下面是开始你过一种方法可以基于保持的初始阵列中的每个整数多少次出现在地图中的计数的想法。一旦所有的数字已经算,按升序值进行排序的地图,然后打印地图的输出:

 进口的java.util.ArrayList;
进口的java.util.HashMap;
进口java.util.TreeMap中;公共类SortCount {
    公共静态无效的主要(字串[] args){
        INT NUMS [] = {0,0,0,1,3,3,2,1,3,5,6,0};
        HashMap的<整数,整数GT;数=新的HashMap<整数,整数GT;();        的for(int i = 0; I< nums.length;我++){
            如果(counts.containsKey(NUMS [
                整数C = counts.get(NUMS [I])+ 1;
                counts.put(NUMS [I],C);
            }
            其他{
                counts.put(NUMS [I],1);
            }
        }        ValueComparator<整数,整数GT; BVC =新ValueComparator<整数,整数GT;(计数);
        TreeMap的<整数,整数GT; SortedMap的=新TreeMap的<整数,整数GT;(BVC);
        sortedMap.putAll(计数);        ArrayList的<整数GT;输出=新的ArrayList<整数GT;();
        为(整数i:sortedMap.keySet()){
            对于(INT C = 0; C< sortedMap.get(I); C ++){
                output.add(ⅰ);
            }
        }        的System.out.println(output.toString());
    }
}

它采用了比较类中的值比较一地图

 进口了java.util.Comparator;
进口的java.util.Map;公共类ValueComparator< T1,T2延长可比< T2>>实现比较< T1> {
    地图< T1,T2>基础;
    公共ValueComparator(地图< T1,T2>基地){
        this.base =基地;
    }    @覆盖
    公众诠释比较(T1 K1,K2 T1){
        T2 VAL1 = base.get(K1);
        T2 val2的= base.get(K2);        返回val1.compareTo(val2的);
    }
}

How can I arrange the elements in an array based on the number of occurrences of that value in ascending order in java.

This is what I've tried:

int a[]={0,0,0,1,3,3,2,1,3,5,6,0};
int b=a.length;
for(int i=0;i<b;i++) {
    for(int j=0;j<i;j++) {
        int temp;
        if( a[j]>a[i]) {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
    }
}

for(int r=0;r<a.length;r++) {
    System.out.println(a[r]);
}

解决方案

Here's one approach to start you off could be based on the idea of keeping a count of how many times each integer in the initial array has occurred in a map. Once all the numbers have been counted, sort the map by order of ascending value, and then print the output of the map:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.TreeMap;

public class SortCount {
    public static void main(String[] args) {
        int nums[] = {0,0,0,1,3,3,2,1,3,5,6,0};
        HashMap<Integer,Integer> counts = new HashMap<Integer,Integer>();

        for(int i = 0; i < nums.length; i++) {
            if(counts.containsKey(nums[
                Integer c = counts.get(nums[i]) + 1;
                counts.put(nums[i], c);
            }
            else {
                counts.put(nums[i],1);
            }
        }

        ValueComparator<Integer,Integer> bvc = new ValueComparator<Integer,Integer>(counts);
        TreeMap<Integer,Integer> sortedMap = new TreeMap<Integer,Integer>(bvc);
        sortedMap.putAll(counts);

        ArrayList<Integer> output = new ArrayList<Integer>();
        for(Integer i : sortedMap.keySet()) {
            for(int c = 0; c < sortedMap.get(i); c++) {
                output.add(i);
            }
        }

        System.out.println(output.toString());
    }
}

Which uses a Comparator class to compare the values in a Map:

import java.util.Comparator;
import java.util.Map;

public class ValueComparator<T1,T2 extends Comparable<T2>> implements Comparator<T1> {
    Map<T1,T2> base;
    public ValueComparator(Map<T1,T2> base) {
        this.base = base;
    }

    @Override
    public int compare(T1 k1, T1 k2) {
        T2 val1 = base.get(k1);
        T2 val2 = base.get(k2);

        return val1.compareTo(val2);
    }
}

这篇关于数组排序的基础上按升序排列出现计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆