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 › int and float in collections
Page Index Toggle Pages: 1
int and float in collections (Read 691 times)
int and float in collections
May 25th, 2005, 6:46am
 
Since Java collections stores objects, I would have to make ints and floats into Integer and Float before putting them into, say, a linked list right?

It seems a little convoluted and I'm just wondering if the creation of the new objects would create inefficiencies. Is there a simple way around this? Or would the resultant bytecode generated by Processing be optimized anyway.

e.g.
ArrayList al;
al.add(new Integer(i));
LinkedList ll;
ll.addFirst(new Float(f));
Re: int and float in collections
Reply #1 - May 25th, 2005, 5:16pm
 
it's going to be really inefficient to use the objects, and create lots of temporary objects like that. that's one of the things with java that tends to make programs slow.

if you just need a list of ints or floats, you're better off using just an array and keeping track of the count:

Code:

int xcount;
float x[] = new float[10];

void add(float what) {
if (xcount == x.length) {
x = expand(x, xcount * 2);
}
x[xcount++] = what;
}

then when using them:

for (int i = 0; i < xcount; i++) {
// do something with x[i]
}
Page Index Toggle Pages: 1