嗨,我想显示 int[] 数组中出现的数字。我发现很多人试图问类似的问题,但他们的数组是字符串。这是我的工作从 10-25 中选择 10 个随机数并打印 100 次,显示数字的出现。例如:
12 15 16 17 20 14 24 21 22 15 10 12 16 24 23 14 23 12 11 10
10 的频率是:2 11 的频率是:1 12 的频率是:3 依此类推
public void pickRandomNo(int[] a) {
printHeader();
Random randomGenerator = new Random();
for (int row = 0; row < 9; row++) {
for (int i = 0; i < 10; i++) {
array[i] = 10 + randomGenerator.nextInt((25 - 10) + 1);
System.out.print(array[i] + " ");
} System.out.println("");
}
}
public void displayOccurences() {
ArrayList arrlist = new ArrayList();
arrlist.add(array);
int freq = Collections.frequency(arrlist, 10);
System.out.println("Frequency of 10 is: " + freq);
}