Trying to create a method that will make an array and assign different numbers from 1 - 9 to each index of the array (nos. cannot be repeated). please have a look at the boolean comparison i made, this is where the cmd stuck at runtime. I have tried to separate the two but nothing happens.
Please help.
import java.util.ArrayList;
class Testing {
public static void main (String[] args) {
ArrayList<Integer> grid = new ArrayList<Integer>();
int randNum;
int count = 0;
int size=8;
for (int b = 0; b <=size; b++) {
while (count <= size) {
randNum = (int) (Math.random() * 9);
if (grid.contains(randNum) == false & randNum != 0 ) {
grid.add(randNum);
count++;
}
}
System.out.println(grid.get(b));
}
System.out.println("size: " + grid.size());
}
}
Why not use a Fisher-Yates shuffle instead?
That is, fill the array with 1-9 and then Collections.shuffle()
it.
Aside: use &&
not &
in your original code.