cdiem
February 11th, 2010, 06:32 PM
I'm trying to make a simple snake clone for fun. I got stuck when trying to update the drawing of the snake on the screen. The problematic part is adding a node to a queue (similar to the following):
int[] test = new int[2];
Queue<int[]> dummyQueue = new LinkedList<int[]>();
for (int i = 0; i < 3; i++) {
test[1] += 50;
dummyQueue.add(test);
// show me some results:
for (int[] node : dummyQueue) {
System.out.print(Arrays.toString(node));
}
System.out.println();
}
This outputs:
[0, 50]
[0, 100][0, 100]
[0, 150][0, 150][0, 150]
Why does adding an element to the queue change every other element in the queue? Is this an expected behaviour? How would you rewrite it, so that it shows:
[0, 50]
[0, 50][0, 100]
[0, 50][0, 100][0, 150]
Basically I need a queue of tuples of (x,y) coordinates - but the above outputs surprised me. Please, help.
int[] test = new int[2];
Queue<int[]> dummyQueue = new LinkedList<int[]>();
for (int i = 0; i < 3; i++) {
test[1] += 50;
dummyQueue.add(test);
// show me some results:
for (int[] node : dummyQueue) {
System.out.print(Arrays.toString(node));
}
System.out.println();
}
This outputs:
[0, 50]
[0, 100][0, 100]
[0, 150][0, 150][0, 150]
Why does adding an element to the queue change every other element in the queue? Is this an expected behaviour? How would you rewrite it, so that it shows:
[0, 50]
[0, 50][0, 100]
[0, 50][0, 100][0, 150]
Basically I need a queue of tuples of (x,y) coordinates - but the above outputs surprised me. Please, help.