问题描述
我无法把随机数到阵列在我的测试类。在code是在java中。我不能这样做单独的,因为最终我将有多达600价值,填补了数组。下面是测试类:
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:
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");
}
}
我已经通过测试类和它调用的函数另一类走了,没有错误。我只是需要把随机值放入数组莫名其妙。任何意见将是多少AP preciated。
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.
如果你看看在code,你可以看到,我已经做了一个小功能,用于生成数字本身。我只是不知道该怎么做,这样的数字将进入一个数组。
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.
推荐答案
您只需要改变循环内的线随机数分配给数组的当前索引。
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 Arrays tutorial thread for everything you need to know about creating, initializing, and accessing arrays.
顺便说一句,而不是重复 1000
,你的循环条件或许应该去到尺寸
。
By the way, instead of repeating 1000
, your loop condition should probably go up to size
.
for (int i = 0; i < size; i++)
这篇关于填充我的阵列,随机数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!