我正在尝试编写一个应用程序,该应用程序将使用随机数填充数组。似乎一切正常,直到我尝试在我的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%确定我的循环具有正确的初始化和终止操作符,因为长度等于十。
我真的很难过,但像大多数情况下,我确信它是一个非常简单的答案。我的输出如下:
Inputted array is length: 10
0 //The array is not populated with numbers, so all indexes of the array return zero.任何和所有的帮助,不胜感激。