tags:

views:

47

answers:

2

I really wish Processing had push and pop methods for working with Arrays, but since it does not I'm left trying to figure out the best way to remove an object at a specific position in an array. I'm sure this is as basic as it gets for many people, but I could use some help with it, and I haven't been able to figure much out by browsing the Processing reference.

I don't think it matters, but for your reference here is the code I used to add the objects initially:

Flower[] flowers = new Flower[0];

for (int i=0; i < 20; i++)
{
    Flower fl = new Flower();
    flowers = (Flower[]) expand(flowers, flowers.length + 1);
    flowers[flowers.length - 1] = fl;
}

For the sake of this question, let's assume I want to remove an object from position 15. Thanks, guys.

+1  A: 

I think that your best bet is to use arraycopy. You can use the same array for src and dest. Something like the following (untested):

// move the end elements down 1
arraycopy(flowers, 16, flowers, 15, flowers.length-16);
// remove the extra copy of the last element
flowers = shorten(flowers);
MPG
A: 

You may also want to consider using ArrayList which has more methods available than a plain array.

You can remove the fifteenth element by using myArrayList.remove(14)

Brendan