【发布时间】:2012-02-11 01:28:14
【问题描述】:
我正在尝试编写一个应用程序,它将用随机数填充一个数组。在我尝试在 populateArray 方法中输入 for 循环之前,一切似乎都运行良好。
import java.util.Random;
public class PerformanceTester {
//takes an empty array, but the size must be allocated BEFORE passing
//to this function. Function takes a pre-allocated array and input size.
public static int[] populateArray(int[] inputArray, int n) {
//Create the number generator
Random generator = new Random();
int length = inputArray.length;
System.out.println("Inputted array is length: " + length);
for (int i = 0; i == length; i++) {
// for debugging purposes: System.out.println("For loop entered.");
int random = generator.nextInt((2 * n) / 3);
// for debugging purposes: System.out.println("Adding " + random + " to the array at index " + i);
inputArray[i] = random;
}
return inputArray;
}
public static void main(String[] args) {
int[] input;
input = new int[10];
int[] outputArray = populateArray(input, 10);
System.out.print(outputArray[0]);
}
}
如我的输出所示,编译器清楚地进入了该方法(在第 29 行调用时),但似乎在到达 for 循环时停止了所有执行。我 100% 确定我的循环具有正确的初始化和终止运算符,因为长度等于 10。
老实说,我很难过,但与大多数情况一样,我确信这是一个非常简单的答案。我的输出如下:
Inputted array is length: 10
0 //The array is not populated with numbers, so all indexes of the array return zero.
非常感谢任何和所有帮助。
【问题讨论】: