# Thread: Moving elements in a vector

1. Member Join Date
Nov 2011
Location
New Hampshire
Posts
29
Rep Power
0

## 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.

Java Code:
```    if(ke.getKeyCode() == KeyEvent.VK_UP)
{
for(int i = 0;i<5;i++)
{
body.get(i).setLocation(xLoc,yLoc);
xLoc = body.get(i).getXLocation();
yLoc = body.get(i).getYLocation();
}
}```  Reply With Quote

2. ## Re: Moving elements in a vector

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?  Reply With Quote

3. Member Join Date
Nov 2011
Location
New Hampshire
Posts
29
Rep Power
0

## 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]  Reply With Quote

4. ## Re: Moving elements in a vector

Can you post a small program that compiles and executes and shows your problem?  Reply With Quote

5. Member Join Date
Nov 2011
Location
New Hampshire
Posts
29
Rep Power
0

## 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.

Java 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 Vector<Integer> body;

private int direction = 2;

public Snakes ()
{

body = new Vector<Integer>();

for(int i= 0; i<5; i++)
{
tailPiece = i*12;
}

}
public void keyTyped(KeyEvent e)
{
}

public void keyPressed(KeyEvent ke)
{
if(ke.getKeyCode() == KeyEvent.VK_UP)
{

for(int i = 0;i<5;i++)
{
int x = body.get(i);

System.out.println("Vector value at i: " +x);
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();

}
}```
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  Reply With Quote

6. ## Re: Moving elements in a vector

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.
[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.
Last edited by Norm; 12-06-2011 at 03:06 AM.  Reply With Quote

games, homework, java, moveshapes, vectors 