【0,1,2,3,4,5,6,7,8,9】
举一个例子,以此类推..
package shuai.study.frequency; /** * @author shengshu * */ public class NumberFrequency { private final int len = 10; private int[] numberArray = null; private int[] frequencyArray = null; public NumberFrequency() { numberArray = new int[len]; frequencyArray = new int[len]; } // Initialize numberArray public void initNumberArray() { for (int i = 0; i < len; i++) { numberArray[i] = i; } } // Get final frequencyArray public int[] getFrequencyArray() { while (isSetFrequencyArray(numberArray)) { isSetFrequencyArray(numberArray); } return frequencyArray; } // Whether set frequencyArray according to judging 'frequencyArray[i] != frequency' public boolean isSetFrequencyArray(int[] numberArray) { boolean setNextFrequencyFlag = false; for (int i = 0; i < len; i++) { int frequency = getFrequency(numberArray[i]); if (frequencyArray[i] != frequency) { frequencyArray[i] = frequency; setNextFrequencyFlag = true; } } return setNextFrequencyFlag; } // Get numberArray's element(number) frequency public int getFrequency(int number) { int frequency = 0; for (int i = 0; i < len; i++) { if (frequencyArray[i] == number) { frequency++; } } return frequency; } // Print array public void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + "\t"); } } // Main thread public static void main(String[] args) { NumberFrequency numberFrequency = new NumberFrequency(); numberFrequency.initNumberArray(); int[] frequencyArray = numberFrequency.getFrequencyArray(); numberFrequency.printArray(frequencyArray); } }
Calculate Number Frequency,布布扣,bubuko.com
原文:http://blog.csdn.net/three_man/article/details/38235941