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

Java 用随机数填充我的数组?,java,arrays,random,int,Java,Arrays,Random,Int,在我的测试类中,我很难将随机数放入数组。代码是用java编写的。我不能单独做,因为最终我将不得不用多达600个值填充数组。以下是测试类: import java.util.Random; public class test { /** * @param args */ public static void main(String[] args) { int size = 1000; int max = 5000;

在我的测试类中,我很难将随机数放入数组。代码是用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");

    }
}
import java.util.Random;
公开课考试{
/**
*@param args
*/
公共静态void main(字符串[]args){
int size=1000;
int max=5000;
int[]数组=新的int[size];
int循环=0;
随机生成器=新随机();
//写一个循环,生成1000个整数和
//使用generator.nextInt(max)将它们存储在阵列中
generator.nextInt(max);//正在生成一个
//我需要生成1000个
//所以我需要一种循环,可以产生1000个数字。

对于(inti=0;i,您只需更改循环内的行即可将随机数分配给数组的当前索引

array[i] = generator.nextInt(max);
查看教程线程,了解有关创建、初始化和访问阵列的所有信息

顺便说一下,不要重复
1000
,您的循环条件可能会增加到
size

for (int i = 0; i < size; i++)
for(int i=0;i
自1.8以来

Arrays.setAll(array, i -> {
    return generator.nextInt(max);
});
这将使用介于0(包含)和
max
(独占)之间的随机数填充数组

进一步阅读:及


话虽如此,我还是喜欢使用蜥蜴比尔提供的方法,使用循环来初始化每个值。

@MariaStewart你知道为什么有必要吗?问问自己:如果你不把生成的值放在数组中,它们是如何进入数组的?如果你使用代码,插入,或者我认为它“神奇地工作”。