배열에 임의의 숫자를 채울 응용 프로그램을 작성하려고합니다. 내 populateArray 메서드에서 for 루프를 입력 할 때까지 모든 것이 잘 작동하는 것처럼 보입니다.자바 컴파일러가 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 루프에 도달하면 모든 실행을 중지하는 것으로 보입니다. 저는 길이가 10이기 때문에 제 루프에 적절한 초기화 및 종료 연산자가 있는지 100 % 확신합니다.
저는 솔직히 엉망입니다. 그러나 대부분의 경우와 마찬가지로 나는 매우 간단한 답변을 확신합니다. 내 결과는 다음과 같습니다.
Inputted array is length: 10
0 //The array is not populated with numbers, so all indexes of the array return zero.
모든 도움을 주실 수 있습니다.
'<='길이로 설정하면 ArrayIndexOutOfBoundsException이 발생합니다. –