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 › removing elements of an arrayList
Page Index Toggle Pages: 1
removing elements of an arrayList (Read 6036 times)
removing elements of an arrayList
Jun 4th, 2010, 2:01am

I want to remove an element of an array list equal to a specific value.
ie: i want to remove the value 1 (not the index) from the arrayList [1, 2].

//list[1, 2]
//valueToRemove = 1;  
for (int i=0; i<list.size(); i++) {
 if (i == valueToRemove) {


The value with index equal to i is being removed not the value 1.

I also tried to convert to a string;
String value= str(i);

Thanks for any help.
Re: removing elements of an arrayList
Reply #1 - Jun 4th, 2010, 2:20am
for (int i=0; i<list.size(); i++) {
int val = list.get(i);
if (val == valueToRemove) {
Re: removing elements of an arrayList
Reply #2 - Jun 4th, 2010, 3:02am
i assume that the line
int val = list.get(i);  cast's the object coming out of the list?

i get an error, highlighting the above line of code:
"cannot convert from Object to int"

what does that mean?

Re: removing elements of an arrayList
Reply #3 - Jun 4th, 2010, 4:38am
My code was generic, since you didn't show what kind of data you put in the list.
Assuming it holds Integer objects, indeed, you have to do:
int val = (Integer) list.get(i);
Re: removing elements of an arrayList
Reply #4 - Jun 4th, 2010, 8:06am
Adding to what PhiLho said:

There are two versions of the remove() method for lists.  One takes an object as the argument and removes that object from the list.  The other takes an int, and removes the object with that index from the list.  I think the confusion you're having here is that the objects you're trying to remove are ints or Integers (the object version of int), so it's tricky to make sure you're getting the right remove() method.  
Re: removing elements of an arrayList
Reply #5 - Jun 4th, 2010, 8:11am
yes, it is integer objects.

thankyou very much, works perfectly.
Re: removing elements of an arrayList
Reply #6 - Jun 4th, 2010, 8:24am
further to Smitty's post:
i didn't realise there were two versions of remove(), thought i would check this out - but there is no explanaition of remove() in the reference/language page of processing website?

Also, don't get the difference between int and integer; thought the same? There is an explanaition of int and int() but not of "integer" in the reference/language page.

Where could i find some explanation of these please?
Re: removing elements of an arrayList
Reply #7 - Jun 4th, 2010, 8:56am
The java ArrayList docs linked to from the Processing reference page includes a list of "Methods inherited from interface java.util.List" including the remove(Object) method...

As for the distinction between int and Integer this goes some way towards explaining it; though doesn't satisfactorily explain why we need these different types.  I think I'll leave that to the Java experts Wink
Re: removing elements of an arrayList
Reply #8 - Jun 4th, 2010, 9:52am
Mmm, I forgot the remove(Object) method.
It can be selected as:
  ArrayList list = new ArrayList();

list.remove(new Integer(7));

Indeed, the distinction between int and Integer is becoming blurry since Java does auto-boxing/un-boxing (ie. when needed it automatically wraps an int in an Integer and reciprocally).

chunkyorkshireman, that's Integer with a capital I to mark the fact that's a class. Why we have both? Partly because collections like ArrayList or HashMap know only to manipulate objects, not primitive types like int. So the latter must be wrapped in objects, bloating them but making them more flexible... Smiley
Re: removing elements of an arrayList
Reply #9 - Jun 7th, 2010, 7:07am
Thanks for all the help and explanation.

from blindfish's link;
"An Integer, is a Object that contains a single int field. .....It is sort of like a Fedex box to contain the int."

Regarding remove():
I am using this to remove elements in an arrayList called "associates". An element is removed from associates when found.
An arrayList of agents wander around and when they find one of their associates, that associate is removed from their associate list.

println("id: " + id + ", who: " + who);  
//id is the arrayList of agents, who is the particular agent of that arrayList
found = space[i].home.id;         //id of nest-associate found
//agents are a class, which with another class (home) form a composite class (space).
println("found: " + found);
for (int j=0; j<associates.size(); j++) {
 int val = (Integer) associates.get(j);  
 if (val == found) {
 associates.remove(new Integer(val));    //remove from list
println("associates left: " + associates);

I want the element to be removed from the arrayList associates of the agent that has found the associate only - so each agent has to find all its associates, removing them from the list as they find them BUT what is actually happeing is that the find is being removed from the associate list of all the agents in the same arrayList.
I hope that makes sense?
Does anyone know why this might be happening? Happy to post any code you might need to help - just not sure what to post!
Re: removing elements of an arrayList
Reply #10 - Jun 7th, 2010, 9:18am
If I've understood correctly your issue might be down to how you create the agent's copy of the ArrayList.  It could be that you're setting a reference to the original array rather than creating a true copy.  If it's a reference then when you change it from within the agent the changes are being made to the referenced ArrayList - i.e. the original ArrayList - and this is referenced from all agents.  This is a common issue with arrays and has definitely been discussed here previously...

IIRC you need to use a specific method to copy an Array or ArrayList (possibly clone()) rather than simply assigning it to another ArrayList variable.

There's one other thing to watch out for with remove() when iterating over an ArrayList.  You'll notice that PhiLho's code included a 'break' when a value was removed.  IIRC that's significant as you may run into problems if you continue to iterate over the ArrayList after an object has been removed...  It's safer to mark an object for removal and remove it after the iteration has completed.

I don't pretend to understand the technicalities here; but from my experience the iteration is carried out over a 'static' version of the ArrayList that doesn't get updated should you remove an element from it; so you can potentially try and retrieve an item that has in fact been removed: something that's obviously best avoided!
Re: removing elements of an arrayList
Reply #11 - Jun 18th, 2010, 7:21am
i'm still stuck on this Shocked

i think, as blindfish says , i am "setting a reference to the original array rather than creating a true copy".

my code:
ObjectA [] objecta= new ObjectA[objecta];
void setup() {
//data is loaded from an xml file

objecta[i] = new ObjectA(id, color(H, S, B), area, new PVector(position.x, position.y));     //defines ObjectA
 //add associates to arrayList of ObjectA and ObjectB
class ObjectA{
 ObjectB objectb;
 ArrayList objectCs;
 PVector origin;
 ArrayList associates = new ArrayList();
 int objectcCount = population;

 ObjectA(int id, int colour, int Size, PVector loc) {
   objectb= new ObjectB (id, colour, Size, loc);                
   objectCs = new ArrayList();
   origin = loc.get();
   for (int i = 0; i < population; i++) {
     objectCs.add(new objectC(id, Size, origin, false, i, associates));
//i think this is were i go wrong passing the associates to objectCs      
class ObjectB {
 int id;
 color colour;
 int Size;
 int x;
 int y;
 PVector position;
 ArrayList associates = new ArrayList();

 ObjectB(int identity, int c, int area, PVector loc) {  
   id = identity;
   colour = c;
   Size = area;
   position = loc.get();
class objectC{
 ArrayList associates = new ArrayList();

objectC(int identity, int Size, PVector loc, boolean mode, int whom, ArrayList asso) {
 associates = asso;

ObjectA is composed of ObjectB and ObjectC. ObjectCs is an arrayList.
how can i copy the arrayList associates of either ObjectA or ObjectB to each ObjectC?

Page Index Toggle Pages: 1