Stefan
Stefan

Reputation: 1895

Java array add new value to the first and remove the last

I'm trying to update my array so if you add something to the first array tail[0] The whole array updates and moves 1 to the next and at the end the last one tail[49] gets removed.

So if I add a value to tail[0] I want tail[1] to get the old tail[0] value, tail[2] to get the old tail[1] value and tail[3] to get the old tail[2] value. the value of the old tail[3] will get removed. This is like a array push where you remove the last value each time.

I tried this so far but I can't manage to get it to work.

Create array:

Tail[] tail = new Tail[50];

Give the whole array a value:

for(int i = 0; i < tail.length; i++){
    tail[i] = new Tail(0,0);
}

Update the array so it adds a new value and removes the last value.

public void tail(){

    tail[0] = new Tail(ball.getX(),ball.getY());

    for(int i = 1; i < tail.length; i++){
        tail[i] = new Tail(tail[i-1].x,tail[i-1].x);
    }
}

Tail class:

public class Tail {

    public float x;
    public float y;

    ShapeRenderer shapeRenderer;
    SpriteBatch batch;

    public boolean active = false;

    public Tail(float x, float y){
        shapeRenderer = new ShapeRenderer();
        batch = new SpriteBatch();

        this.x = x;
        this.y = y;

          batch.begin();
          shapeRenderer.begin(ShapeType.Filled);
          shapeRenderer.circle(x, y, 16);
          shapeRenderer.setColor(Color.BLACK);
          shapeRenderer.end();
          batch.end();  

        Gdx.app.log("TailX"+x, "TailY"+y);
    }

}

The tail void ends up crashing my game whats the best way to solve this problem?

Upvotes: 1

Views: 3755

Answers (5)

MAV
MAV

Reputation: 7457

First, it seems like you're creating a new Tail when you attempt to move the tails. Any reason for that? You should be able to re-use the same instances, by just moving the reference.

Also, in your code you're trying to insert the new element at position 0, before moving all the other elements. You need to do it the other way around, to make sure only the last item is removed. Should be doable with something like this:

for(int i = tail.length - 2; i >= 0; i--){
    tail[i + 1] = tail[i];
}
tail[0] = new Tail(ball.getX(),ball.getY());

This way you are just moving the Tail instances, rather than creating new ones. Unless I misunderstood something, this should work.

It does sound like you could benefit from using a Queue instead. A Queue is designed to insert elements in one end and retrieve items from the other end.

If you used a LinkedList (it implements the Deque interface) instead of an array, you should be able to do something like this:

tail.addFirst(new Tail(ball.getX(), ball.getY()));
tail.removeLast();

Upvotes: 1

Neeraj Jain
Neeraj Jain

Reputation: 7730

This functionality is similar to FIFO . So you can use Queue to implement this functionality .

enter image description here

Object firstElement = queue.remove();

To remove elements from a queue, you call the remove() method. This method removes the element at the head of the queue. In most Queue implementations the head and tail of the queue are at opposite ends

Queue queueA = new LinkedList();

queueA.add("element 0");
queueA.add("element 1");
queueA.add("element 2");

The add() method adds the element to the tail

Now if you call queueA.remove();

it will give the first element inserted which is

element 0

Now use this and implement your own logic .

Upvotes: 1

Sagar D
Sagar D

Reputation: 2618

Try the following code (Updated)

public void tail(){

    float tempx = ball.getX();
    float tempy = ball.getY()

    for(int i =(tail.length - 1); i > 0; i--){
        tail[i].x = tail[i-1].x;
        tail[i].y = tail[i-1].y;

    }
    tail[0].x = tempx;
    tail[0].y = tempy;
}

Upvotes: 1

NDY
NDY

Reputation: 3557

You should not use an array for it. Use something like a Stack that could work like the following:

Stack stack = new Stack();

//you add elements to stack using push method
stack.push("Test");
stack.push("Test2");

Upvotes: 0

Ofer Lando
Ofer Lando

Reputation: 824

Don't use arrays for something like this - use Stack instead.

Upvotes: 1

Related Questions