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 › Deleting from an array of objects
Page Index Toggle Pages: 1
Deleting from an array of objects (Read 727 times)
Deleting from an array of objects
Feb 19th, 2008, 8:54pm
 
I'm trying to delete an item from the middle of an array of objects, but the code i'm using keeps erroring.

basically, i've tried to split the array into 2 pieces, around the index of the item to be removed, and then join the two pieces back together. However when I use the splice command, i get a ClassCastException.

I thought I was using it right, but clearly not.

here's my code

Code:

//f is the instance to be removed from the array
stem[] arrayP1 = (stem[]) subset(stems,0,f);
stem[] arrayP2 = (stem[]) subset(stems,f);
stems = (stem[]) splice(arrayP1,arrayP2,arrayP1.length);
//stem[] stems = (stem[]) splice(arrayP1,arrayP2,arrayP1.length); doesn't work either...



How can I make this work, or is there an easier way to do it all together?

Thanks

Martin
Re: Deleting from an array of objects
Reply #1 - Feb 19th, 2008, 9:30pm
 
Weird. Same exception is throwed here.

Edit: This deserves a bug report, see:
http://dev.processing.org/bugs/show_bug.cgi?id=734

By the way, I found something that looks like a bug : subset(array, offset) doesn't return what we except when using an array of objects... It should return the same than subset(array, offset, array.length - offset) but doesn't.

Edit: The subset() bug has already been reported and should be fixed in version 0136, see:
http://dev.processing.org/bugs/show_bug.cgi?id=707

Anyway, you have 2 other solutions :
- shift every element in your array after the one you want to delete (which will be overwritten), and use the shorten() method.
- use a Java Collection, like java.util.ArrayList with its useful add() and remove() methods.


Re: Deleting from an array of objects
Reply #2 - Feb 19th, 2008, 9:41pm
 
EDIT: thanks for the reply antiplastik, i wrote this without checking if anyone had replied first... I'll see if i can work out ArrayList, I've seen it mentioned elsewhere.


Ok, after a bit of looking round (that I admittedly should have spent longer on before posting originally) i found the concat function.

This avoids the problem of the ClassCast problem, however it seems to do something strange and I can't pin down what's happening. Basically, what I think should happen is that if a particular stem has reached 600, it should remove that from the array, but leave the others. However, on running it, when one reaches that point, they all get deleted.

I presume this has something to do with program flow, but i can't work it out.

Code:

void update(){
println(stems.length);
for (int f=0;f<stems.length;f++){

if (stems[f].rY>600){
//we should delete if from the array
stem[] arrayP1 = (stem[]) subset(stems,1,f);
stem[] arrayP2 = (stem[]) subset(stems,f+1);
println(arrayP1.length);
println(arrayP2.length);
stems = (stem[])concat(arrayP1,arrayP2);
println(stems.length);
break;
}
stems[f].update();
}
}


Any help would be appreciated.

Thanks
Martin
Re: Deleting from an array of objects
Reply #3 - Feb 19th, 2008, 9:50pm
 
As mentionned above, there's a bug in version 0135 with the subset() method when you don't specify the third argument (length).

Try to replace subset(stems,f+1) with subset(stems, f+1, stems.length - (f+1)). This should be fixed in version 0136.

Re: Deleting from an array of objects
Reply #4 - Feb 19th, 2008, 10:02pm
 
check out this thread for more info on arrayLists. I've been using them ever since.
Re: Deleting from an array of objects
Reply #5 - Feb 19th, 2008, 10:03pm
 
Yup, changed it to what you suggested and it's perfect now.

Thanks antiplastik, it seems this place would fall down without your support (i know i certainly would have a while ago)

@dek, I'll be using them in the future i think, they look very handy
Re: Deleting from an array of objects
Reply #6 - Feb 19th, 2008, 10:09pm
 
You're too kind ;-)

By the way, for those who would like to use ArrayLists to easily remove elements from a list while iterating it (as you did when checking if the value > 600), you can't do that using java.util.ArrayList.remove() method. You must iterate your list with an iterator and use the java.util.Iterator.remove() method, otherwise a Concurrent Modification Exception may be thrown.
Page Index Toggle Pages: 1