We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › easy way to remove object from an array
Page Index Toggle Pages: 1
easy way to remove object from an array? (Read 1052 times)
easy way to remove object from an array?
Jun 7th, 2005, 4:52am
 
Say we have:

Code:

thing thingArray[];

void setup()
{
size(100,100);
thingArray=new thing[200];
for(int i=0;i<thingArray.length;i++)
thingArray[i]=new thing();
}
void draw()
{

}
class thing
{
float data;
thing()
{}
thing(thing t)
{
this.data=t.data;
}
}

thing[] append(thing t[],thing newThing)
{
thing temp[]=new thing[t.length+1];
System.arraycopy(t,0,temp,0,t.length);
temp[t.length]=new thing(newThing);
return temp;
}



The append function is meant to demonstrate how I usually add something to the end of an array, regardless of object. I usually just write one of these handy append functions for every object I make that needs this sort of thing.

Now my question is.. is there an equally smart way to write a function that removes a particular object from an array, then reduce the array size by one? Say something like:

thingArray=remove(thingArray,8);

That function should remove the eigth element (thingArray[7]) from the array.

I can think of a few ways... but they are all horrible and cumbersome.. including cutting the array in half.. then reattatching it. Ew.

Anyone have a better solution? Cheesy

Re: easy way to remove object from an array?
Reply #1 - Jun 7th, 2005, 5:30am
 
something like this will work:

Code:
int[] remove(int array[], int item) {
for (int i = item+1; i < array.length; i++) {
array[i-1] = array[i];
}
return contract(array, array.length-1);
}


tho that's a little sloppy, so maybe:

Code:
int[] remove(int array[], int item) {
int outgoing[] = new int[array.length - 1];
System.arraycopy(array, 0, outgoing, 0, item);
System.arraycopy(array, item+1, outgoing, item, array.length - (item + 1));
return outgoing;
}

(not tested, hastily off the top off my head)
Re: easy way to remove object from an array?
Reply #2 - Jun 7th, 2005, 11:40am
 
push pop would be great, I tried wrinthing something like that a few days ago but I'm not processing-smart enough Smiley
Re: easy way to remove object from an array?
Reply #3 - Jun 7th, 2005, 10:18pm
 
My lecturer at university made us use a java example of removing an element (in a collection clasS) and in that code he used the same method as fry's first method of running a for loop moving all the elements down a position.

I think thats quite a suitable solution; so long as you're not doing it every frame shifting the memory patterns of an array won't take up any more memory then the size of the array. It just takes up a bit of processing time, which compared to things like drawing has to very small surely?
Re: easy way to remove object from an array?
Reply #4 - Mar 27th, 2006, 6:11am
 
This thread has been REALLY helpful in my understanding of array management (especially when using arrays of objects). I've also really been desiring a push and pop method for arrays. I've included an example below, but it doesn't return the popped off element, since I'm not sure how to return two different items.

Code:
String[] l = new String[0];
int counter = 0;
boolean remove = false;

void setup(){
size(200,200,P3D);
background(0);
}

void draw(){
}

void keyPressed(){
remove = true;
}

void keyReleased(){
remove = false;
}

void mousePressed(){
if(remove && l.length > 0){
l = pop(l);
} else {
l = push(l, str(counter));
counter += 1;
}
print(l);
println();
}

String[] push(String[] a, String element){
String[] b = new String[a.length+1];
b[0] = element;
System.arraycopy(a,0,b,1,a.length);
return b;
}

String[] pop(String[] a){
String[] b = new String[a.length-1];
System.arraycopy(a,0,b,0,a.length-1);
return b;
}
Re: easy way to remove object from an array?
Reply #5 - Mar 27th, 2006, 7:49am
 
Technically you can't return two variables, however you can pass a pointer of the element-type to the function, then have the function make that pointer point to the object that was popped off.

Thanks for contributing!
Page Index Toggle Pages: 1