# Moving elements in a vector

• 12-06-2011, 01:51 AM
dru36
Moving elements in a vector
Hey guys,

I'm creating a snake game. My snake is made from a vector of shapes (the shape I am using is a custom shape class). The snake is implementing a keylistener.

When I call a move method on my snake I want the head, which is a separate element from the body, to move and each subsequent element in the vector to take the place of the one in front of it.

So I would like the first element in the vector to be at the heads previous location and the second element in the vector to be at the first, etc etc.

At the moment I'm using this method.

Code:

```    if(ke.getKeyCode() == KeyEvent.VK_UP)     {         xLoc = head.getXLocation();         yLoc = head.getYLocation();         head.setLocation(xLoc, yLoc-12);       for(int i = 0;i<5;i++)       {         body.get(i).setLocation(xLoc,yLoc);         xLoc = body.get(i).getXLocation();         yLoc = body.get(i).getYLocation();       }     }```
• 12-06-2011, 02:04 AM
Norm
Re: Moving elements in a vector
Quote:

each subsequent element in the vector to take the place of the one in front of it.
Have you read the API doc for the Vector class?
Do any of its methods look like they will do what you want?
• 12-06-2011, 02:34 AM
dru36
Re: Moving elements in a vector
Well I think the get method will be fine, I'm just writing my method wrong. I am assigning each element in the vector to a single location, but I can't figure out the semantics to fix it.

I have a point A that is being adjusted. Each time point A is adjusted to point B, I want a vector of points to be adjusted like this

[A] Vector[1,2,3,4,5]

After

[B] Vector [A,1,2,3,4]

But right now it looks like this

[A] Vector[1,2,3,4,5]

after

[B] Vector[A,A,A,A,A]
• 12-06-2011, 03:12 AM
Norm
Re: Moving elements in a vector
Can you post a small program that compiles and executes and shows your problem?
• 12-06-2011, 03:40 AM
dru36
Re: Moving elements in a vector
Here is the basic problem I'm having. It's not my program but it shows the area that I need assistance in.

You can see here that I'm assigning the values of all the components in the vector to the head value instead of assigning them the next position in the vector.

Code:

``` import java.awt.event.*; import java.util.*; import java.awt.Point; import java.awt.Color; public class Snakes implements KeyListener {     private Point  last,  next;     private int head,  tailPiece;     private Vector<Integer> body;     private int xLoc, yLoc, headX, headY;     private int direction = 2;     public Snakes ()     { head = 100;                         body = new Vector<Integer>();             for(int i= 0; i<5; i++)     { tailPiece = i*12;       body.add(tailPiece);     }     }     public void keyTyped(KeyEvent e)     {     }     public void keyPressed(KeyEvent ke)   {            if(ke.getKeyCode() == KeyEvent.VK_UP)     {         head += 10;       for(int i = 0;i<5;i++)       {         int x = body.get(i);         x = head;                 System.out.println("Vector value at i: " +x);         System.out.println("Snakes Head: " + head);         System.out.println("Integer i: " + i);       }     }     }     public void keyReleased(KeyEvent e)     {     }     public static void main(String[]args)     {       Frame f = new Frame();       Snakes s = new Snakes();       f.addKeyListener(s);           } }```
Also, here is the list it prints out on the first key stroke UP.

Vector value at i: 110
Integer i: 0
Vector value at i: 110
Integer i: 1
Vector value at i: 110
Integer i: 2
Vector value at i: 110
Integer i: 3
Vector value at i: 110
Integer i: 4
• 12-06-2011, 04:01 AM
Norm
Re: Moving elements in a vector
Quote:

I'm assigning the values of all the components in the vector to the head value instead of assigning them the next position in the vector.
Where is the head value and where is the next position?

The only methods I see you are using are the add and get methods.
Quote:

[A] Vector[1,2,3,4,5]

After

[B] Vector [A,1,2,3,4]

In this example you gave the difference between A and B is that 5 was removed from the end of the Vector and A was inserted at the beginning of the Vector.

Go read the API doc and see what methods will do that.

Then write a 10 line program to test out the methods. Those 10 lines include two printlns to show the before and after contents of the Vector.