java怎么把随机数放入数组_java – 用随机数填充我的数组?

我在测试类中将随机数放入数组时遇到了麻烦.代码在

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

}

}

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

如果您查看代码,您可以看到我已经创建了一个小函数来自己生成数字.我只是不知道如何做到这一点,以便数字将进入一个数组.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值