I have a List that I sorted and I want to convert the List
to a Color[]
One reason I have these colors in a list is so that I could implement my own comparison to order the colors in the List. It only contains colors. Now that I can sort them I want to turn the List into a Color[]
. So far I have tried different ways of convert it but have been unsuccessful. The reason I use a list is because I sort the colors based on coordinates that correspond with the current color. Here is more background:
Here are my attempts:
SortColors s = new SortColors();
s.x = roi.x;
s.y = roi.y;
s.color = colorToAdd;
Here is its ordering class:
public class SortColors implements Comparable<SortColors> {
public int x, y;
public Color color;
@Override
public int compareTo(SortColors other) {
if (null==other) throw new NullPointerException();
// implement the logic, like:
// this will compare X, unless difference in Y is more than EG 10
return Math.abs(y - other.y) > 10 ? y - other.y : x - other.x;
}
@Override
public String toString(){
/* String result = "";
result = "x " + x + ", y " + y + "\n" + color.toString() + "\n";
System.out.println(result);
*/
return color.toString();
}
}
1. The casting failed on this one:
Color [] sortedColors = (Color[]) colorsToSort.toArray(); //List to a Object Array
Here is another:
2 (This was gave me an ArrayStore exception)
Object [] sortedColors = colorsToSort.toArray(); //List to an Object Array
Color[] sorted = Arrays.copyOf(sortedColors, sortedColors.length, Color[].class);
3 I also tried this but get the ArrayStoredException:
Color [] sortedColors = colorsToSort.toArray(new Color[54]); //List to a Color Array