-3

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
ProgrammingCuber
  • 377
  • 4
  • 14
  • Possible duplicate of [How to convert object array to string array in Java](https://stackoverflow.com/questions/1018750/how-to-convert-object-array-to-string-array-in-java) – Andrew Li Aug 19 '17 at 19:44
  • If you look at the answer it only works with a String []. I tried that answer and it wasn't working. @AndrewLi – ProgrammingCuber Aug 19 '17 at 19:45
  • 1
    "*The only reason I have these colors in a list is so that I could implement my own comparison to order the colors in the List.*" you can also sort arrays with your own Comparator via `sort` methods from `java.util.Arrays` class. Anyway you can return from List array of specific type with `list.toArray(new Color[0]);`. – Pshemo Aug 19 '17 at 19:45
  • I also tried that but I got the ArrayStore exception @Pshemo – ProgrammingCuber Aug 19 '17 at 19:47
  • 1
    [ArrayStoreException documentation](https://docs.oracle.com/javase/7/docs/api/java/lang/ArrayStoreException.html) "*indicate that attempt has been made to store the wrong type of object into an array of objects*" so you are trying to place into array something which doesn't belong there. Are you sure that your list contains only colors? It is hard to help you without seeing proper [mcve]. – Pshemo Aug 19 '17 at 19:49
  • Possible duplicate of [Converting 'ArrayList to 'String\[\]' in Java](https://stackoverflow.com/questions/4042434/converting-arrayliststring-to-string-in-java) – derkoe Aug 19 '17 at 19:51
  • 1
    Why do you have the class `SortColors`? You need a class that implements `Comparator`, not one that implements ``. Or make `Color` implement `Comparable`. – Bohemian Aug 19 '17 at 20:09
  • Does your list contain instances of `SortColors`? If yes then it is no surprise that you can't put them in `Color` array since `SortColors` *is not* Color, it *holds* Color. Do you want to get that held Colors and put them in array? – Pshemo Aug 19 '17 at 20:11
  • I am sorry @Bohemian I apologize, I haven't use the Comparables and Comparator before and the only time I had used it was now, I didn't know exactly what I was doing, I apologize, I was just looking for help to find a solution to a problem I wasn't familiar with :/ – ProgrammingCuber Aug 20 '17 at 02:15

1 Answers1

1

You said

The only reason I have these colors in a list is so that I could implement my own comparison

Don't use a list. Use an array from the start and use the Arrays.sort(T[] a, Comparator<? super T> c) utility method to sort them:

Color[] colors; // given an array of colors 
Arrays.sort(colors, (x, y) -> Math.abs(y - other.y) > 10 ? y - other.y : x - other.x);

or if Color implements Comparable<Color> with your comparison calculation, use the Arrays.sort(Object[] a) utility method:

Arrays.sort(colors);

In the unlikely event that you absolutely must use a list:

Color[] colorArray = Arrays.stream(colorList).map(Color.class::cast).toArray(Color[]::new);
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • I use this to sort my List `Collections.sort(colorsToSort);`. How would I do this differently to work with your answer? – ProgrammingCuber Aug 19 '17 at 19:52
  • @ProgrammingCuber see addition to my answer – Bohemian Aug 19 '17 at 19:56
  • I wouldn't use a List but I would have to add more to my question to show you why I use a list. – ProgrammingCuber Aug 19 '17 at 19:59
  • @ProgrammingCuber Or just remove information that you are using list just because of sorting ability since arrays also have it via `Arrays.sort` method. If you have more reasons to use list then "The **only** reason I have these colors in a list is so that I could implement my own comparison" is simply not true and as such should not be in your question. – Pshemo Aug 19 '17 at 20:02
  • @ProgrammingCuber then add more to your question – Bohemian Aug 19 '17 at 20:02
  • You are right @Pshemo I made a mistake in that phrase I am updating the question currently. – ProgrammingCuber Aug 19 '17 at 20:03
  • Updated the question – ProgrammingCuber Aug 19 '17 at 20:06
  • @ProgrammingCuber You still do not need a list. And you don't need your `SortColors` class at all. You can code the essential part of the comparison `Math.abs(y - other.y) > 10 ? y - other.y : x - other.x` as a lambda when calling `Arrays.sort` - see edited answer. – Bohemian Aug 19 '17 at 20:11
  • I see what you mean but the lambda now, what would I do in the place of `other` now that I don't need the class? – ProgrammingCuber Aug 19 '17 at 23:01