Reputation: 79
I'm trying to get my loop to go through the array 16 times. When I use upCollisions[i], it doesn't work but when I use upCollisions[0] or any other index of the array, it works. I can't understand why it is not working using the for loop.
Here's my code:
public void handleUpArrow()
{
int upCollisions[] = {15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0,};
for(int i =0; i < 16; i++)
{
if(goldenBallPosition == upCollisions[i])
{
}
else
{
jBGrid[goldenBallPosition].setIcon(imageSand);
jBGrid[goldenBallPosition -16].setIcon(imageBall);
goldenBallPosition -= 16;
jBCompass.setIcon(imageCompassNorth);
jTDirection.setText("N");
jTSquare.setText((""+goldenBallPosition));
}
}
}
Upvotes: 0
Views: 78
Reputation: 8353
There are some problems with your code. First of all it's better to make the work in the if
branch, and not in the else
. Then I think the problem could be in this line of code:
goldenBallPosition -=16;
If I do undestand correctly your code, it could help if you use a loop like this
for (int i = 15; i >= 0; i--)
Don't you receive an ArrayOutOfBoundException
during execution?
Upvotes: 1