我正在嘗試編寫一個應用程序,該應用程序將用隨機數填充數組。似乎一切正常,直到我嘗試在我的populateArray方法中輸入for循環。Java編譯器不會進入我的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.
任何和所有的幫助,非常感謝。
'<='長度將在這裏導致ArrayIndexOutOfBoundsException,我想。 – 2013-03-24 17:56:33