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 › Storing ints in an ArrayList
Page Index Toggle Pages: 1
Storing ints in an ArrayList (Read 373 times)
Storing ints in an ArrayList
Nov 4th, 2008, 9:25pm
 
Is there a way I can store and access integers in an ArrayList? I input ints into an ArrayList, but when I try to access them and store them in other variables, ArrayList tries to return objects since it doesn't know what it's storing. I'm doing something like this:

ArrayList nodeTimers;
nodeTimers = new ArrayList();
nodeTimers.add(0,0);
int timers = (int) nodeTimers.get(0);

It won't let me cast the element as an integer. Is there a way I can do this? The whole reason I'm using an ArrayList in the first place is because I wanted to be able to easily remove elements in the array without shifting around the other contents. If anyone knows an easy way to do this with a regular old array, that would be helpful too.


Re: Storing ints in an ArrayList
Reply #1 - Nov 5th, 2008, 12:39am
 
ArrayList stores objects.. and int gets autoboxed into Integer, an Object type rather than a primitive type.

So you have to do something like:

ArrayList nodeTimers=new ArrayList();
nodeTimers.add(0,0);
int timer=((Integer)nodeTimers.get(0)).intValue();
Page Index Toggle Pages: 1