Я пытаюсь написать приложение, которое будет заполнять массив случайными числами. Кажется, все работает нормально, пока я не попытаюсь ввести цикл for в моем методе populateArray.
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.
Любая помощь очень ценится.