Reputation: 124
I have a piece of code:
public void setBoardMemberPosition() {
int[] position = null;
do{
positionX = RandomNumberGenerator.generateRandomNumber(Board.DIMENSION);
positionY = RandomNumberGenerator.generateRandomNumber(Board.DIMENSION);
position = new int[] { positionX, positionY };
} while(checkIfPositionIsOccupied(position));
board.setElementAt(positionX, positionY, name);
storedMembers.add(position);
}
Can someone explain to me why the elements are not being added to the list, where:
private ArrayList<int[]> storedMembers = new ArrayList<int[]>();
is a Class variable and:checkIfPositionIsOccupied(position)
returns true/false
if (storedMembers.contains(position))
i have a feeling its something to do with the do while
, ive debugged it with no luck.
Upvotes: 1
Views: 71
Reputation: 34146
If you want to add the array position
to the ArrayList
on each iteration, your add()
call must be inside the loop:
do {
// ...
storedMembers.add(position);
} while(...);
Edit:
for (int i = 0; i < storedMembers.size(); i ++)
// ...
if (!checkIfPositionIsOccupied(position))
storedMembers.add(position);
}
Upvotes: 2
Reputation: 2943
Because you don't add in your while loop anything to the list?!
Upvotes: 0