As has been said in the comments by @Slaw and @Anonymous:
The algorithm that your teacher is after is (partly pseudocode):
while (remainingElements > 0) {
int i = <random index>;
if (board[i] != 0) {
print board[i];
board[i] = 0;
remainingElements--;
}
}
For picking a random index into your board you need to use random.nextInt(board.length)
.
Notice that we only decrease remainingElements
when a number is picked and printed, not every time through the loop. This causes the loop to repeat more times than there are elements in the board
array. Eventually the random index will also pick the last remaining elements, remainingElements
will be decreased to 0 and the loop will terminate.