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 › get or toArray to copy floats from ArrayList
Page Index Toggle Pages: 1
get or toArray to copy floats from ArrayList (Read 1516 times)
get or toArray to copy floats from ArrayList
Jul 22nd, 2009, 1:44pm
 
Hi,

I am adding a number of floats to an ArrayList, which need to copy into an array later. Currently I am doing it with this line of code.

float  xPosCount[k] = ((Float)thatXPos.get(k)).floatValue();    

As such it works, but it seems that it doesn't copy them in the order they were in the Arraylist but sorts them into -  smallest value first - highest value last. Is that correct? I was trying to use toArray() instead and put my code like that

float  xPosCount[k] = ((Float)thatXPos.toArray(k)).floatValue();    

but this doesnt work. I tried lots of other things but cant work it out. Some help would be great.
Re: get or toArray to copy floats from ArrayList
Reply #1 - Jul 22nd, 2009, 2:54pm
 
I fear I don't understand how the first line can work, I haven't tried but I would bet you would get a syntax error there (in the declaration float xPosCount[k]).
I don't see either how your data gets magically sorted...

The second line is even more perplexing...
Perhaps something like:
float[] xPosCount = new float[thatXPos.size()];
thatXPos.toArray(xPosCount);

could work (untested).

[EDIT] Fixed hasted code against syntax errors, but it doesn't work. Sad
Re: get or toArray to copy floats from ArrayList
Reply #2 - Jul 22nd, 2009, 3:09pm
 
Like PhiLo says, things don't get sorted automatically. To convert an ArrayList into an Array of floats, you could do it like this:

Code:

ArrayList list = new ArrayList();

..add numbers into list...

/* Convert ArrayList into Array of Objects */  
Object[] temp = list.toArray();

/* create an empty Array of floats */
float[] fList = new float[list.size()];

for (int i = 0; i < temp.length; i++) {
   /*
   First Cast each Object into Float.
   There is a second --implicit-- cast
   from object Float to type float,
   this happens automatically.
   */
   fList[i] = (Float) temp[i];
 }
Re: get or toArray to copy floats from ArrayList
Reply #3 - Jul 23rd, 2009, 1:16am
 
alvaro is right, although I would have used (float) instead of (Float).
My code above doesn't work, Java fails to convert an ArrayList of boxed primitive types to an array of this primitive type...
A variant of alvaro's code:
Code:
ArrayList thatXPos = new ArrayList();
// Use autoboxing feature of Java 1.5
for (int i = 0; i < 10; i++) thatXPos.add(random(50));

int len = thatXPos.size();

float[] xPosCount = new float[len];

Float[] fa = new Float[len];
thatXPos.toArray(fa);
for (int i = 0; i < len; i++)
{
// Using auto unboxing
xPosCount[i] = fa[i];
}

for (int i = 0; i < len; i++) println(xPosCount[i]);

Hey, auto(un)boxing is one of the rare Java 1.5 features we can use (since it doesn't change syntax), so I won't miss it!  Wink
Re: get or toArray to copy floats from ArrayList
Reply #4 - Jul 29th, 2009, 4:35am
 
thanks, very helpful, got it working.
Page Index Toggle Pages: 1