Java 用随机数填充我的数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16205651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 05:46:36  来源:igfitidea点击:

Filling my arrays with random numbers?

javaarraysrandomint

提问by Maria Stewart

I'm having trouble putting random numbers into the arrays in my test class. The code is in java. I can't do it individually, because eventually I'll have to fill the arrays with as many as 600 values. Here is the test class:

我无法将随机数放入我的测试类中的数组中。代码在java中。我不能单独做,因为最终我必须用多达 600 个值填充数组。这是测试类:

import java.util.Random;

public class test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        int size = 1000;
        int max = 5000; 
        int[] array = new int[size];
        int loop = 0; 

        Random generator = new Random();
        //Write a loop that generates 1000 integers and 
        //store them in the array using generator.nextInt(max)

        generator.nextInt(max); //generating one

        //I need to generate 1000
        //So I need some kind of loop that will generate 1000 numbers. 
        for (int i =0; i<1000; i++)
        {
            generator.nextInt(max);
        }

        /**
         * After I do this, I'll have the array, array. 
         * Then comes what's under this. 
         * THat method is for measuring the time.
         * System.currentTimeMillis();, 
         * with this, I can collect a time for the start of the method
         * and one for the end. 
         * Time at the end, minus the time at the start
         * gets us the running time. 
         */

        long result;

        long startTime = System.currentTimeMillis();
        sort.quickSort(array,  100,  array.length-1);

        long endTime = System.currentTimeMillis();
        result = endTime-startTime; 

        System.out.println("The quick sort runtime is " + result + " miliseconds");

        long result2;

        long startTime2 = System.currentTimeMillis(); 
        sort.partition(array, 100, array.length-1); 
        long endTime2 = System.currentTimeMillis();
        result2 =  endTime2 - startTime2;
        System.out.println("The partition runtime is "+result2 + " miliseconds");

        long result3;

        long startTime3 = System.currentTimeMillis();
        sort.bubbleSort(array, 100);
        long endTime3 = System.currentTimeMillis();
        result3 = endTime3-startTime3;
        System.out.println("The bubble sort runtime is "+result3 + " miliseconds");

        long result4;

        long startTime4 = System.currentTimeMillis();
        sort.selectionSort(array, 100); //change the second number to change
        //the size of an array. 
        long endTime4 = System.currentTimeMillis();
        result4 = endTime4-startTime4;
        System.out.println("The selection sort runtime is "+result4 + " miliseconds");

    }
}

I've already gone through the test class and the other class that it calls functions from, and there are no errors. I just need to put random values into the arrays somehow. Any advice would be much appreciated.

我已经完成了测试类和它从中调用函数的另一个类,并且没有错误。我只需要以某种方式将随机值放入数组中。任何建议将不胜感激。

If you take a look at the code, you can see that I've already made a small function for generating the numbers themselves. I just don't know how to do that so that the numbers will go into an array.

如果你看一下代码,你会发现我已经做了一个小函数来自己生成数字。我只是不知道如何做到这一点,以便数字将进入一个数组。

采纳答案by Bill the Lizard

You just need to change the line inside the loop to assign the random number to the current index of the array.

您只需要更改循环内的行以将随机数分配给数组的当前索引。

array[i] = generator.nextInt(max);

Check out the Arraystutorial thread for everything you need to know about creating, initializing, and accessing arrays.

查看数组教程线程,了解有关创建、初始化和访问数组的所有信息。

By the way, instead of repeating 1000, your loop condition should probably go up to size.

顺便说一句,1000您的循环条件可能应该达到,而不是重复size

for (int i = 0; i < size; i++)

回答by Schalk

Since 1.8

从 1.8

Arrays.setAll(array, i -> {
    return generator.nextInt(max);
});

This fills your array with random numbers between 0(inclusive) and max(exclusive).

这会用 0(包含)和max(不包含)之间的随机数填充您的数组。

Further reading: The Arrays Classand The IntUnaryOperator Interface

进一步阅读:数组类IntUnaryOperator 接口

That being said and done, I'd prefer to use the approach provided by Bill the Lizard, using a loop to initialize each value.

话虽如此,但我更愿意使用 Bill the Lizard 提供的方法,使用循环来初始化每个值。