Sort

edited November 2017 in Questions about Code

Hi,

I have a code that is suposed to work, but it has an error "Cannot find anything named Array" in line 9

 import java.util.HashSet;
Integer[] getPalette(PImage img){
HashSet<Integer> colours = new HashSet<Integer>();
img.loadPixels();
for(int p : img.pixels){
  colours.add(new Integer(p));
}
Integer[] p = colours.toArray(new Integer[colours.size()]);
Arrays.sort(p);
return p;  
}

void colour (PImage img, Integer p[]){
  Integer c;
  float distance;
  float mindistance = 1000000;
  Integer closest=0;   
  //colors.add (new ObjectColor (theimage.get(1,1)));
  for (int i = 1; i < img.width; i++){
    for (int j = 1; j < img. height; j++){
  //println (i+"         "+j);      
      c= img.get(i,j);
      //found = false;
      for (int k = 0; k < p.length; k++){

        distance = sq(red(p[k])-red(c)) + sq(green(p[k])-green(c)) + sq(blue(p[k])-blue(c));
        if (distance < mindistance){  
          mindistance=distance;
          closest = p[k];
        }
      }
      set(i,j,closest); 
      mindistance=100000;
    }
  }   
}   

Answers

  • Line 9:

    Arrays.sort(p);
    

    Where is Arrays defined?

  • edited November 2017 Answer ✓

    Need to add

    import java.util.Arrays;

  • Thank you, Its strange because I remember Ive used this code years ago, and it worked and now I have to add this

  • edited November 2017

    Processing uses a pre-proccessor to convert the Sketch code to pure Java. Newer versions have reduced the number of imports added.

  • Bad idea I think, because processing is good to start. To learn. If you have to add imports, this is more difficult to understand

  • edited November 2017

    Well, I see what you mean, but you can also sort an array without importing Array...

    https://processing.org/reference/sort_.html

    ...or you can use the no-import ArrayList or one of the types like IntList, StringList etc. all of which have a .sort()

    https://processing.org/reference/IntList.html

    ...so I'm not sure that a Processing 3 beginner doesn't really have options here for sorting an array without importing.

Sign In or Register to comment.