2

I have 2 methods.

 public static <T extends Comparable<T>> T[] sort(T[] array) {}

And

 public static <T extends Comparable<T>> T[] sort(T[] array,
            Comparator<? super T> c) {}

Implemantation w/o any erros, but after JUnit test case i got an error on T[] s =...

JUnit says:

java.lang.object cannot be cast to java.lang.Comparable;

In the test case JUnit gives me also this line next to the error:

assertArrayEquals(a1, ComparisonCountingSort.sort(ar1));

My question here is to know what i can fix here to have an succesful JUnit test case.

James Rich
  • 129
  • 1
  • 3
  • 8
  • 1
    Take a look at this post: http://stackoverflow.com/questions/1817524/generic-arrays-in-java. Now, what are you trying to do with the Array s? Is that code in your productive code or in the Unit test? – Martin Nov 07 '13 at 10:29
  • No,its the productive code. I post the Unit test case in start post.Thanks – James Rich Nov 07 '13 at 10:36
  • @JamesRich you should accept one of the answers as correct if they worked for you. – Ruslans Uralovs Nov 12 '13 at 11:40

3 Answers3

3

If you want an array of nulls of the correct type and length

T[] s = Arrays.copyOf(Arrays.copyOf(array, 0), array.length);

If you are ok with an exact copy, you can use

T[] s = Arrays.copyOf(array,  array.length);
Alan Spencer
  • 566
  • 3
  • 12
  • I tested the second one. It works fine. Can you explain why it works in the methods? – James Rich Nov 07 '13 at 11:39
  • Under the hood, this method also uses 'System.arraycopy'. The trick is how it creates an array with a generic type... This is done by finding the _Component Type_ of the incoming array 'array.getClass().getComponentType()' and dynamically creating a new array of the same type 'Array.newInstance(componentType)' - I find it quite useful to dive into the JDK source once in a while to understand what is really going on, and maybe using a JUnit test and/or debugger to see the details... – Alan Spencer Nov 07 '13 at 12:36
  • To clarify... This approach actually creates an array of the correct type, and in the calling code an attempt to cast the array to the non-generic type will work. If you 'new Comparable(...' This is actually a _Comparable_ array - you can add _any_ Comparable to it, so, you can't cast it to a specific type as it may contain something that is not that type... – Alan Spencer Nov 07 '13 at 13:00
1

Since T is a type which extends Comparable it should be

@SuppressWarnings("unchecked")
T[] s = (T[]) new Comparable[array.length];

Reason being an Object type cannot be cast to Comparable since Object IS NOT A Comparable

sanbhat
  • 16,864
  • 6
  • 46
  • 62
0

Collections of object cannot be casted to collection of T. What is more you cannot do this: T x = new T(). You can provide dependency to class that is able to create object of type T. To copy your array you can use System.arraycopy

Areo
  • 868
  • 5
  • 11
  • Thanks for the answers. I dont get the last part of your post. Maybe i post the whole implementation,maybe it is easier for me to understand. – James Rich Nov 07 '13 at 10:31