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 › ArrayList trouble...
Page Index Toggle Pages: 1
ArrayList trouble... (Read 922 times)
ArrayList trouble...
Feb 24th, 2009, 3:29pm
 
I'm having trouble with  toArray method because of casting problems.
with the following code (sample extracted from my sketch)
i always get this error:
"The method toArray(Object[]) in type ArrayList is not applicable for the arguments (int[])"

the line:

"color cor[]=(color[])unico.toArray(new color[unico.size()]);"

i copied from an example that uses a String[], but i do not understand this "(color[])" kind of cast between parenthesis. Also trying to access the ArrayList (ArrayList_name.get(0);)give me the same kind of problem...  
Please help. How is this conversion or casting of data done?
thanks


CODE:

ArrayList unico;

color x =  color(0);


void setup(){
 unico = new ArrayList();


}

void draw(){

unico.add(x);
color cor[]=(color[])unico.toArray(new color[unico.size()]);
println ("size is "+unico.size());
println (unico);
}


Re: ArrayList trouble...
Reply #1 - Feb 25th, 2009, 12:00am
 
color isn't a class/object but a primitive type, an int.
Why do you do this conversion?
Re: ArrayList trouble...
Reply #2 - Feb 25th, 2009, 5:37am
 
I'm trying to count how many different colors are in pixels array. So i created an array to add unique numbers after testing each pixel trough pixels[]. I didn't really understood how append() works so i was trying ArrayList.add() then converting it to an array i could sort. Are ArrayList only for objects, opps i missed that. Meanwhile i understood append() and did it with regular arrays. The code is a following,  but is veeeery slow. I'm sure i must be doing it in a stupid way. My goal is to sort unique colors of an image. That's another problem, how to sort colors? I mean colors have 3 dimensions, using sort() it sorts the hex representation of color right? It's not good. I'd like to sort first by hue and then by luminance or maybe I will need to make it in  a 3d enviroment. But for now i still counting the unique pixels as you can see. How to make it faster?
thanks for help

CODE:

Quote:
//THE OUTPUT IS TO CONSOLE ONLY
PImage img;
int unique[] = new int[1];
int a=0, i=0;
boolean repeated=false;


void setup(){
 img=loadImage("ana.png");
 size(img.width,img.height);
 image(img,0,0);
 loadPixels();
 unique[0]=pixels[0];
}


void draw(){
 println("min-"+minute()+" sec-"+second());//<< for performance measure
 while(a<pixels.length){
   while(i<unique.length){
     if (pixels[a]==unique[i]){
       i=unique.length;
       repeated =true;
     }
     else {        
       i++;
     }
   }
   if(!repeated){
     unique=append(unique,pixels[a]);
     a++;
     i=0;
   }
   else{
     a++;
     i=0;
     repeated = false;
   }
 }
 println("min-"+minute()+" sec-"+second());//<< for performance measure
 print("there are "+unique.length+" different colors");
 noLoop();
}








Re: ArrayList trouble...
Reply #3 - Feb 25th, 2009, 1:01pm
 
Wow. Lot of sort out.
First, there is a thread about color sorting: Sort 2d array. There is also a thread about counting colors in an image.
"converting it to an array i could sort"
You can sort ArrayList too. I started to write a Hacks entry about sorting arrays and collections, as the topic surfaces often. I should finish it... :-)
"The code is a following,  but is veeeery slow."
Yes, append() is handy, but extremely slow on large arrays: each time you use it, it copies the entire array to a new one just one unit larger...
"Are ArrayList only for objects"
Yes, that's an irritating limitation of Java Collections. They made objects boxing primitive types mostly for this usage (Integer, Float, Boolean, etc.). Java 1.5 introduced autoboxing to reduce the pain. But it is still costly (size and time). That's why arrays are still useful: small (minimal size), fast access. Good when you know the final size.
"I'm trying to count how many different colors are in pixels array."
Actually, you don't need sorting and such. I would use a HashMap with (boxed) color as keys and occurrence number (boxed too) as value: take color, get it from HashMap. If null, not there yet, put (color, 1) in map. If there, put (color, nb + 1).
Re: ArrayList trouble...
Reply #4 - Feb 27th, 2009, 7:37pm
 
PhiLho, thanks for your time.
Would ArrayList be faster?
I have never used HashMap. I'll give it a try. thanks
Re: ArrayList trouble...
Reply #5 - Mar 1st, 2009, 3:09am
 
Generally speaking, an ArrayList is fast if you are storing and looking up items by a numerical index and a HashMap is fast if you are storing and looking up things by an object identifier.

Philho's recommendation sounds correct to me for your case, since the key is a Color object and the value is a count. Since the value must also be an object, the Integer count has to be converted to an int to increment it, of course, as PhiLho indicated.
Page Index Toggle Pages: 1