Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 20个从0到10的随机数数组。如何计算其中的具体数字?_Java_Arrays_Random_Numbers_Counting - Fatal编程技术网

Java 20个从0到10的随机数数组。如何计算其中的具体数字?

Java 20个从0到10的随机数数组。如何计算其中的具体数字?,java,arrays,random,numbers,counting,Java,Arrays,Random,Numbers,Counting,我制作了这个数组,但我在数数字。我可以用“如果”十次,但对我来说似乎是错的。也许这里最好使用循环“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 < arra

我制作了这个数组,但我在数数字。我可以用“如果”十次,但对我来说似乎是错的。也许这里最好使用循环“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] + ",");
        }
    }
}
import java.util.Random;
公共类zadanie2{
公共静态void main(字符串[]args){
int数组[];
数组=新整数[20];
for(int i=0;i
由于您没有声明其他内容,我假设您只需要打印值:

    IntStream.range(0, 10)
          .forEach(n -> System.out.println(n + "->" + Arrays.stream(array)
                                                            .filter(i -> i == n)
                                                            .count()));

您没有存储每个随机数的出现次数,此外,您正在每个迭代中创建一个新的
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<>();
    // 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");
    });
}

请注意,这些示例不会在相同的范围内创建相同的数字。

正如@deHaar所评论的,您可以使用
映射来实现这一点:

import java.util.Map;
导入java.util.Random;
导入java.util.TreeMap;
公共类CountNum{
公共静态void main(字符串[]args){
//创建数组和随机实例
int[]数组=新的int[20];
Random rd=新的Random(System.currentTimeMillis());
//创建地图以统计出现次数
映射计数=新树映射();
//用随机数填充数组并计算
//一次性事件。。。
for(int i=0;i
这张照片

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);

int输入阵列[];
inputArray=新整数[20];
随机rd=新随机();
HashMap elementCountMap=新HashMap();
for(int i=0;i
输出:

输入数组:[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}


希望此解决方案有效。

您到底想实现什么目标?是否要计算所有随机数的出现次数?如果是,请使用
映射
,并将数字用作键,将其出现次数用作值。我想让我的程序把它们列成一个列表,类似这样:1发生x次;2例发生x次;等等…请不要在每次迭代时创建一个新的
随机
。谢谢,但我需要一段时间来理解这一点。。。。大概一两天吧。你知道我在哪里可以找到一些关于HashMap的合适信息吗?请不要在每次迭代中创建一个新的
随机的
。你可以从oracle官方网站上查看关于HashMap的更多合适信息。链接是。@MauricePerry我已经更新了我的代码,所以现在新的
Random
不会在每次迭代中创建。这是可行的,但是第二个
for
循环是不必要的。如果你能在第一个循环中完成所有事情,为什么还要重复两次呢?数数数字不需要完全填充数组。谢谢!这一条似乎更容易理解:)你能推荐一些好的网站让我了解HashMaps和TreeMap吗?@santana011也许这个网站可以帮助你?
    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);