20个0到10的随机数数组,如何统计其中的具体数字?
20 random numbers array from 0 to 10. How to count specific numbers in it?
我制作了这个数组,但我正在努力计算数字。我可以通过使用 "IF" 10 次来做到这一点,但对我来说这似乎是错误的。也许循环 "for" 最好在这里使用,但我不知道如何处理这个问题。
import java.util.Random;
public class zadanie2 {
public static void main(String[] args) {
int array[];
array = new int[20];
for (int i = 0; i < array.length; i++) {
Random rd = new Random();
array[i] = rd.nextInt(10);
System.out.print(array[i] + ",");
}
}
}
因为你没有另外说明,我假设你只需要打印这些值:
IntStream.range(0, 10)
.forEach(n -> System.out.println(n + "->" + Arrays.stream(array)
.filter(i -> i == n)
.count()));
您没有存储每个随机数的出现次数,此外,您在每次迭代中都创建了一个新的 Random
,这是不应该做的。
如果你想存储出现的事件,那么为它定义一个合适的数据结构,否则你将无法存储它们。我用过一个Map<Integer, Integer>
,看这个例子:
public static void main(String[] args) {
// define a data structure that holds the random numbers and their count
Map<Integer, Integer> valueOccurrences = new TreeMap<>();
// define a range for the random numbers (here: between 1 and 10 inclusively)
int minRan = 1;
int maxRan = 10;
for (int i = 0; i < 20; i++) {
// create a new random number
int ranNum = ThreadLocalRandom.current().nextInt(minRan, maxRan + 1);
// check if your data structure already contains that number as a key
if (valueOccurrences.keySet().contains(ranNum)) {
// if yes, then increment the currently stored count
valueOccurrences.put(ranNum, valueOccurrences.get(ranNum) + 1);
} else {
// otherwise create a new entry with that number and an occurrence of 1 time
valueOccurrences.put(ranNum, 1);
}
}
// print the results
valueOccurrences.forEach((key, value) -> {
System.out.println(key + " occurred " + value + " times");
});
}
作为替代方案,您可以使用 Random
,但对所有迭代使用一个实例:
public static void main(String[] args) {
// define a data structure that holds the random numbers and their count
Map<Integer, Integer> valueOccurrences = new TreeMap<>();
// create a Random once to be used in all iteration steps
Random random = new Random(10);
for (int i = 0; i < 20; i++) {
// create a new random number
int ranNum = random.nextInt();
// check if your data structure already contains that number as a key
if (valueOccurrences.keySet().contains(ranNum)) {
// if yes, then increment the currently stored count
valueOccurrences.put(ranNum, valueOccurrences.get(ranNum) + 1);
} else {
// otherwise create a new entry with that number and an occurrence of 1 time
valueOccurrences.put(ranNum, 1);
}
}
// print the results
valueOccurrences.forEach((key, value) -> {
System.out.println(key + " occurred " + value + " times");
});
}
请注意,这些示例不会在同一范围内创建相同的数字。
正如@deHaar 评论的那样,您可以使用 Map
来做到这一点:
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
public class CountNum {
public static void main(String[] args) {
//create array and Random instance
int[] array = new int[20];
Random rd = new Random(System.currentTimeMillis());
//create Map to count numbers occurrences
Map<Integer, Integer> counts = new TreeMap<>();
//fill array with random numbers and count the
//occurrences in one go...
for (int i = 0; i < array.length; i++) {
array[i] = rd.nextInt(10);
//count inserted number
counts.put(
array[i],
counts.containsKey(array[i]) ? counts.get(array[i]) + 1 : 1
);
}
//print count result:
System.out.println("\n");
for (int i : counts.keySet())
System.out.println("The number " + i +
" was inserted " + counts.get(i) + " times.");
}
}
这会打印
The number 0 was inserted 1 times.
The number 1 was inserted 1 times.
The number 2 was inserted 3 times.
The number 4 was inserted 1 times.
The number 5 was inserted 1 times.
The number 6 was inserted 5 times.
The number 7 was inserted 3 times.
The number 8 was inserted 3 times.
The number 9 was inserted 2 times.
这是为您快速解决的问题。检查以下代码。
int inputArray[];
inputArray = new int[20];
Random rd = new Random();
HashMap<Integer, Integer> elementCountMap = new HashMap<Integer, Integer>();
for (int i = 0; i < inputArray.length; i++) {
inputArray[i] = rd.nextInt(10);
}
for (int i : inputArray) {
if (elementCountMap.containsKey(i)) {
elementCountMap.put(i, elementCountMap.get(i) + 1);
} else {
elementCountMap.put(i, 1);
}
}
System.out.println();
System.out.println("Input Array : " + Arrays.toString(inputArray));
System.out.println("Element Count : " + elementCountMap);
输出:
输入数组:[9, 7, 3, 0, 8, 6, 3, 3, 7, 9, 1, 2, 9, 7, 2, 6, 5, 7, 1, 5]
元素数:{0=1、1=2、2=2、3=3、5=2、6=2、7=4、8=1、9=3}
希望这个解决方案有效。
我制作了这个数组,但我正在努力计算数字。我可以通过使用 "IF" 10 次来做到这一点,但对我来说这似乎是错误的。也许循环 "for" 最好在这里使用,但我不知道如何处理这个问题。
import java.util.Random;
public class zadanie2 {
public static void main(String[] args) {
int array[];
array = new int[20];
for (int i = 0; i < array.length; i++) {
Random rd = new Random();
array[i] = rd.nextInt(10);
System.out.print(array[i] + ",");
}
}
}
因为你没有另外说明,我假设你只需要打印这些值:
IntStream.range(0, 10)
.forEach(n -> System.out.println(n + "->" + Arrays.stream(array)
.filter(i -> i == n)
.count()));
您没有存储每个随机数的出现次数,此外,您在每次迭代中都创建了一个新的 Random
,这是不应该做的。
如果你想存储出现的事件,那么为它定义一个合适的数据结构,否则你将无法存储它们。我用过一个Map<Integer, Integer>
,看这个例子:
public static void main(String[] args) {
// define a data structure that holds the random numbers and their count
Map<Integer, Integer> valueOccurrences = new TreeMap<>();
// define a range for the random numbers (here: between 1 and 10 inclusively)
int minRan = 1;
int maxRan = 10;
for (int i = 0; i < 20; i++) {
// create a new random number
int ranNum = ThreadLocalRandom.current().nextInt(minRan, maxRan + 1);
// check if your data structure already contains that number as a key
if (valueOccurrences.keySet().contains(ranNum)) {
// if yes, then increment the currently stored count
valueOccurrences.put(ranNum, valueOccurrences.get(ranNum) + 1);
} else {
// otherwise create a new entry with that number and an occurrence of 1 time
valueOccurrences.put(ranNum, 1);
}
}
// print the results
valueOccurrences.forEach((key, value) -> {
System.out.println(key + " occurred " + value + " times");
});
}
作为替代方案,您可以使用 Random
,但对所有迭代使用一个实例:
public static void main(String[] args) {
// define a data structure that holds the random numbers and their count
Map<Integer, Integer> valueOccurrences = new TreeMap<>();
// create a Random once to be used in all iteration steps
Random random = new Random(10);
for (int i = 0; i < 20; i++) {
// create a new random number
int ranNum = random.nextInt();
// check if your data structure already contains that number as a key
if (valueOccurrences.keySet().contains(ranNum)) {
// if yes, then increment the currently stored count
valueOccurrences.put(ranNum, valueOccurrences.get(ranNum) + 1);
} else {
// otherwise create a new entry with that number and an occurrence of 1 time
valueOccurrences.put(ranNum, 1);
}
}
// print the results
valueOccurrences.forEach((key, value) -> {
System.out.println(key + " occurred " + value + " times");
});
}
请注意,这些示例不会在同一范围内创建相同的数字。
正如@deHaar 评论的那样,您可以使用 Map
来做到这一点:
import java.util.Map;
import java.util.Random;
import java.util.TreeMap;
public class CountNum {
public static void main(String[] args) {
//create array and Random instance
int[] array = new int[20];
Random rd = new Random(System.currentTimeMillis());
//create Map to count numbers occurrences
Map<Integer, Integer> counts = new TreeMap<>();
//fill array with random numbers and count the
//occurrences in one go...
for (int i = 0; i < array.length; i++) {
array[i] = rd.nextInt(10);
//count inserted number
counts.put(
array[i],
counts.containsKey(array[i]) ? counts.get(array[i]) + 1 : 1
);
}
//print count result:
System.out.println("\n");
for (int i : counts.keySet())
System.out.println("The number " + i +
" was inserted " + counts.get(i) + " times.");
}
}
这会打印
The number 0 was inserted 1 times.
The number 1 was inserted 1 times.
The number 2 was inserted 3 times.
The number 4 was inserted 1 times.
The number 5 was inserted 1 times.
The number 6 was inserted 5 times.
The number 7 was inserted 3 times.
The number 8 was inserted 3 times.
The number 9 was inserted 2 times.
这是为您快速解决的问题。检查以下代码。
int inputArray[];
inputArray = new int[20];
Random rd = new Random();
HashMap<Integer, Integer> elementCountMap = new HashMap<Integer, Integer>();
for (int i = 0; i < inputArray.length; i++) {
inputArray[i] = rd.nextInt(10);
}
for (int i : inputArray) {
if (elementCountMap.containsKey(i)) {
elementCountMap.put(i, elementCountMap.get(i) + 1);
} else {
elementCountMap.put(i, 1);
}
}
System.out.println();
System.out.println("Input Array : " + Arrays.toString(inputArray));
System.out.println("Element Count : " + elementCountMap);
输出:
输入数组:[9, 7, 3, 0, 8, 6, 3, 3, 7, 9, 1, 2, 9, 7, 2, 6, 5, 7, 1, 5]
元素数:{0=1、1=2、2=2、3=3、5=2、6=2、7=4、8=1、9=3}
希望这个解决方案有效。