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 › getting value from Arraylist stored in an object
Page Index Toggle Pages: 1
getting value from Arraylist stored in an object (Read 702 times)
getting value from Arraylist stored in an object
May 27th, 2009, 4:35am
 
Hello i have an array of objects. Each object has one ArrayList with int values

when i want to use these values i get this error:
The method parseInt(boolean) in the type PApplet is not applicable for the arguments (Object)

i seems that im getting and object from the arraylist instead as an int numbers. why is this?
Im accesing to the "position" arraylist that stores integers that is inside my object agents[1] in the following way:

 for(int i=0;i< 111;i++){
  values_x = int(agents[1].positions.get(i));
}

it looks that im getting an object from this but the content of my arraylist positions are integers.

do anybody know how can i do to get the integers instead of objects as data type? ive tried converting with int() but no luck


thanks

P.

Re: getting value from Arraylist stored in an object
Reply #1 - May 27th, 2009, 8:27am
 
First remark: if your array list has a fixed size (the 111 limit of the loop), you should use classical array instead, it will be more efficient (faster, smaller) and you won't have cast problems...
If actually it is dynamic, read on!

Second remark: in ArrayList page there is a comment (easy to overlook!): "An ArrayList doesn't know what it is storing so we have to cast the object coming out"

In clear, yes, ArrayList stores only Objects, ie. undefined blobs of data, without knowing what they really are.
In the Java 1.4 days, you would have get an error by doing al.put(integerVar); because put() wants an object.
With Java 1.5+, the compiler automatically wraps (auto-boxing) the primitive value in the corresponding object, here Integer.
Should we be using 1.5 syntax in Processing, we would have declared ArrayList<Integer> and we could have used int with put() and get() because of the autoboxing.
We cannot, so we have to cast the result of get() to Integer then use intValue() to have the final value:

Integer io = (Integer) agents[1].positions.get(i);
values_x = io.intValue();
Page Index Toggle Pages: 1