-
Problem with a for loop
So I have an array of integers (I'm not allowed to use the java class ArrayList) and I need a method to add a new word to my array.
What I have at the moment is a for loop:
Code:
public void addNewInt(int i) {
for (int a = 0; a < 100; a++) {
if (words[a] == null) {
words[a] = i;
}
}
}
This is adding the integer, but obviously it's adding it to all 100 spaces (when the array is empty which it currently is).
I want the for loop to iterate through my array until it finds an empty space, add the integer to the empty space, but then stop. How can I correct my code to do this, or am I on the wrong lines completely?
Help much appreciated :)
-
-
public void addNewInt(int i) {
for (int a = 0; a < 100; a++) {
if (words[a] == null) {
words[a] = i;
break;
}
}
}
-