Cannot cast Object to Comparable - java

I'm trying to order a binary search tree, and store its values on an array, but when I try to convert my ArrayList to array it says I cannot convert an Object to Comparable.
#Override
public T[] postOrder() {
ArrayList<T> array = new ArrayList<T>();
postOrder(root, array);
return (T[]) array.toArray();
}
private void postOrder(BSTNode<T> currRoot, ArrayList<T> array) {
if (currRoot == null)
return;
if (!currRoot.getLeft().isEmpty())
postOrder((BSTNode<T>) currRoot.getLeft(), array);
if (!currRoot.getRight().isEmpty())
postOrder((BSTNode<T>) currRoot.getRight(), array);
array.add(currRoot.getData());
}
The error message:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
edit: solved that way
public T[] postOrder() {
ArrayList<T> array = new ArrayList<T>();
postOrder(root, array);
return array.toArray((T[]) Array.newInstance(root.getData().getClass(), size()));
}

[Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
This means you are trying to cast Object[] to Comparable[]. (The "[L" means array.)
toArray returns Object[]. You may be able to use <T>toArray(T[]) instead; however, due to type erasure you cannot do
// cannot do
// vvvvv
return array.toArray( new T[ mySize ] );
So either
Your tree needs a Class<T>.
The caller needs to pass a T[] to fill.
postOrder needs to return a non-generic array. (Object[]... Comparable[]...)
You should return a List<T> instead of a T[].
I notice you are using #Override so maybe your supertype (interface, superclass) has instructions on how to implement this method. (i.e. if this is homework you should ask your instructor because it is not clear to me which solution you should use.)
If you happen to have a Class<T>, then you can do
#Override
public T[] postOrder() {
ArrayList<T> array = new ArrayList<T>();
postOrder(root, array);
return (T[]) array.toArray(
java.lang.reflect.Array.newInstance(myClassT, array.size())
);
}
Otherwise you need to change the signature of the method.
See also
How to create a generic array in Java?

Generic arrays are messy, you need to call the other toArray() method:
public T[] postOrder(T[] result) {
ArrayList<T> array = new ArrayList<T>();
postOrder(root, array);
return array.toArray(result);
}
It doesn't matter what size the result is, just make sure you don't use later what you passed in, because it may get reallocated inside toArray if the size is not correct, so use only the return value of postOrder.
Consider that it's probably much much more cleaner if you just return List<T>, and let the caller take care of converting it to an array! Notice how much less explanation is needed for returning a List...
public List<T> postOrder() {
ArrayList<T> array = new ArrayList<T>();
postOrder(root, array);
return array;
}

Related

Why ArrayList<T> can assign to ArrayList<Integer>

public class Demo {
public static void main(String[] args) {
ArrayList<Integer> list = getList();
System.out.println(list.toString());
list.add(1);
System.out.println(list.toString());
}
public static <T> ArrayList<T> getList(){
ArrayList<String> strings = new ArrayList<>();
strings.add("qwert");
return (ArrayList<T>) strings;
}
}
As the code shown above why it is allowed to assign to ArrayList
JDK11 out put
[qwert]
1
Why ArrayList<T> can assign to ArrayList<Integer>
It's because you have provided explicit typecast.
Probably, what you are not able to understand is how ArrayList<Integer> list can hold qwert. Again, it's because you have asked the compiler to trust you that you are putting Integer into it.
Eventually, all the type information is removed when it comes to runtime. You need to understand the concept of Type Erasure.
return (ArrayList<T>) strings;
Here, the String ArrayList is not directly casted to Integer ArrayList, instead, it is casted to T ArrayList. According to 'Target Type Inference' in the docs:
static <T> List<T> emptyList();
List<String> listOne = Collections.emptyList();
This statement is expecting an instance of List; this data type is the target type. Because the method emptyList returns a value of type List, the compiler infers that the type argument T must be the value String.

Java / Generics / Array problems

I would like to understand the relationship between generics and arrays finally, so i will provide an example which is inconsisent for me, based on an ArrayList<T> :
Object[] elementData = new Object[size];
This is where elements of the generic list are stored.
public void add(T element){ elementData[size++] = element; }
public T get(int index) { return (T)elementData[index] }
Completely works. I can get out the underlying <T> objects, however the array which contains the references to these objects is Object.
In contrast to this:
public Object[] toArray()
{
Object[] result = new Object[size];
for(int i = 0;i<size;i++)
{
result[i] = elementData[i];
}
return result;
}
I cannot cast the elements in the returned array to their real type, however the whole set up is the same: an Object array which contains references to <T> objects. I got ClassCastException error, when trying to cast the elements to their real type.
If you look into the Collection interface, you see that there are 2 toArray() methods:
Object[] toArray()
Returns an array containing all of the elements in this collection.
<T> T[] toArray(T[] a)
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.
The reason for that is that you cannot make generic arrays, so you only return a Object[].
Making a generic method of your toArray() is simple:
public <T> T[] toArray(T[] arr)
{
T[] result = arr.size == size ? arr : (T[])java.lang.reflect.Array
.newInstance(arr.getClass().getComponentType(), size);
for(int i = 0;i<size;i++)
{
result[i] = elementData[i];
}
return result;
}
Arrays know their component type at runtime. Therefore, to create an array, you need to provide the component type at runtime. However, in your case you don't know T at runtime. Therefore, you cannot create something that is truly a T[]. You can only return Object[], or rely on someone giving you something with which you can get the type T from at runtime.
You can cast the elements in the returned array to their real type.
But you cannot cast the array to T[ ]
Arrays in java existed before java 5, which introduces Generics.
So Arrays doesn't rely on generics, this is why we often use Collections instead of arrays in API.
So Collection can eventually be casted to Collection, but Object[] cannot be casted to another array T[], it would be another incompatible type.
I think it has to do with the fact that arrays are covariant and thus you are allowed to cast from T[] to Object[], since Object can do anything T can, but the other way around does not make sense, Object can not do everything T can.

Java / Generics / ClassCastException

I have a LinkedList<T> which contains Object toArray() method:
public Object[] toArray()
{
Object[] array = new Object[size];
int c=0;
for(Node<T> i = first;i != null;i=i.next)
{
array[c++] = i.data;
}
return array;
}
I would like to sort my LinkedList with a generic method: <T extends Comparable> void sort (List<T> list). To sort lists, i must represent them as array in this method:
T[] elements = (T[])list.toArray();`
However, i get ClassCastException at this line and i don't know why. Since the generic type of the method equivalent to the element's runtime type in the returned array, this cast is not a lie!
toArray() returns Object[]. The type information is lost and you can't cast it back to T[]. If you want to keep the type information you can use the following. Then you give the method an predifined array which get filled. If you don't give anything - toArray will create a new Object[].
T[] elements = list.toArray(new T[list.size()]);
just filling an array (another writing style):
T[] elements = new T[list.size()];
list.toArray(elements);
or if you use Java 8:
T[] elements = list.stream().toArray(T[]::new);
The method LinkedList.toArray() creates a new array of the type Object[]. It does not create a new array of the type T[]. This is important, because even though the array only contains instances of T, you cannot assign the array to a variable of the type T[], because the array itself has the type Object[]. You can reproduce this error with the following code:
String[] array = (String[]) new Object[0];
If I understand you correctly, you want to convert the list into an array to be able to implement your own search function. Given a List<T>, that contains elements of the type T you want to convert this list into an array of the type T[]. However, you cannot simply call new T[list.size()], since Java looses the generic type information at compile time. To create the correctly typed array, you need to use the reflection method Array.newInstance().
Here is an example:
#SuppressWarnings("unchecked")
private <T extends Comparable<T>> void sort(List<T> list, Class<T> clazz) {
T[] array = list.toArray((T[]) Array.newInstance(clazz, list.size()));
// sort array and write result to the list
}
And here the usage:
List<String> list = new LinkedList<String>();
// populate the list
sort(list, String.class);
System.out.println(list); // -> the sorted list
You should use T[] toArray(new T[list.size()]) instead. No need to cast.

Error with return-statement of generic type function

I am trying to make a generic function into which I pass an array (int[], String[]) and gets back the sorted version of it using TreeSort. I haven't even had the chance to begin to debug it since the mainclass refuses to accept an int[] array as the parameter.
TreeSort
public T[] treeSort(T[] arr) {
T[] result = (T[]) new Object[arr.length];
for (int i = 0; i < arr.length; i++)
if (arr[i] != null)
add(arr[i]);
Iterator it = iterator();
int count = 0;
while (it.hasNext())
result[count++] = (T) it.next();
return (T[]) result;
}
Main
BinarySearchTree<Integer> tree2 = new BinarySearchTree<>();
int[] unsortedNums = {6,3,7,4,1,2,9,8,5};
int[] sortedNums = tree2.treeSort(unsortedNums);
Error
The method treeSort(Integer[]) in the type BinarySearchTree is not applicable for the arguments (int[])
So I attempted to change unsortedNums and sortedNums from int to Integer (why should this matter?), and now running it yields this error:
Errorr 2
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
on line
T[] result = (T[]) new Object[arr.length];
In a generic method in Java, it is not allowed to create an array of the generic type. You are trying to circumvent this rule by creating an Object array and casting it to the generic type, but the compiler is too smart and prevents this. The reason for this rule is that the generic type could well be an Interface, so the runtime wouldn't know which class to create.
For your example, if you really need to keep the unsorted array as it is and return a copy (usually, sorting happens in-place so you would just edit the original array), you can either let the user pass the type of the generic (as in sp00m's answer), or let the user supply the destination array as a parameter so you don't need to create an array yourself.
IMO, the best practice here would be to make your sorting happen in place instead of returning a sorted copy of the data.
Instead of:
T[] result = (T[]) new Object[arr.length];
You have to use:
T[] result = (T[]) Array.newInstance(clazz, arr.length);
Where clazz is the type of the nested elements:
public T[] treeSort(T[] arr, Class<T> clazz) {...}
Finally, when calling, use the boxed primitive:
Integer[] unsortedNums = { 6, 3, 7, 4, 1, 2, 9, 8, 5 };
Integer[] sortedNums = treeSort(unsortedNums, Integer.class);
In Java, arrays know their component types at runtime (the component type is part of the actual runtime array type), and thus you need to specify the component type at runtime to create an array of that type.
#spoom's answer showed you how to create the array given a class object passed into the function. However, you actually already have that information without passing it separately. The parameter arr is of type T[], which means the array object's actual runtime type must be T[] or a subclass thereof. We can extract the component type from that and use it to create the new array:
T[] result = (T[]) Array.newInstance(arr.getClass().getComponentType(), arr.length);
This is how, for example, Arrays.copyOf() and related functions work.
You can't use primitive types (int) in generitcs. Try to use object wrapper, for example
Integer[] unsortedNums = {6,3,7,4,1,2,9,8,5};
Integer[] sortedNums = tree2.treeSort(unsortedNums);
Combining Generics and arrays is problematic in Java; check out the article Restrictions on Generics for details.
As to solving the problem, the easiest thing to do would be not using an array but rather a List. So for example:
public List<T> treeSort(List<T> list) {
List<T> result = new ArrayList<T>(list.size());
for (int i = 0; i < list.size(); i++)
if (list.get(i) != null)
result.add(list.get(i));
Iterator<T> it = list.iterator();
int count = 0;
while (it.hasNext())
result.set(count++, it.next());
return result;
}
Mind you, I haven't tested the logic.

Initializing List<T> with given number of nulls without loop?

Can a List<T> be initialized to contain a given number of nulls, where T is a type parameter of the class of which the list is a member? I sure can do it with a loop, but like to know whether it is possible without.
List<T> myList = new ArrayList<T>(numEls);
creates a list of the given capacity, but size 0, so myList.get(x) fails for all x, and so does, e.g. myList.set(numEls-1,null).
myList = Arrays.asList(new T[numEls]);
does not compile, and
myList = (List<T>) Arrays.asList(new Object[numEls]);
compiles in Eclipse (with an Unchecked cast warning), but not with javac.
Update: Thank you for the answers! However, I found another, quite short, solution close to my last attempt above, which compiles both in eclipse and with our automated build system: Cast the array, not the list!
myList = Arrays.asList((T[]) new Object[numEls]);
If you don't need to mutate the list...
List<T> result = Collections.nCopies(num, (T) null);
... or alternately
List<T> result = new ArrayList<T>(Collections.nCopies(num, (T) null));
You would need to use reflection to instantiate a backing array T[] using Array.newInstance():
public static <T> List<T> getListWithNulls(Class<T> componentType, int length) {
T[] array = (T[])Array.newInstance(componentType, length);
return new ArrayList<T>(Arrays.asList(array));
}
As you can see, this requires a reference to the Class<T> object representing the type of T:
List<String> strListWithNulls = getListWithNulls(String.class, 100);
Also make sure not to confuse the classes java.lang.reflect.Array and java.util.Arrays which are both used here.
Finally, note that reflection is going to be much slower than just using a loop.
Not really a solution, but you wanted to avoid a loop.
void fillNullList(List<T> list, count) {
if (count > 0) {
list.add(null);
fillNullList(list, count - 1);
}
}
Seriously, why do you want to avoid a loop? Probably, you want a solution with O(1) complexity and not a O(n) complexity solution regardless if a loop is used for not.
What you probably want is something like this....
final int maxSize = 50;
List<T> v = new Vector<T>() {{setSize(maxSize);}};
Vectors allow you to set a size, which fills them with null's.
I would just use a loop, its simpler and likely to be faster as well.
List<T> list =
while(list.size()<size) list.add(null);
Any other approach you use is likely to use a loop for you. If this is fine, just write your own method which hides the loop used.
new option with streams:
List resultColumn = IntStream.range(0, 10000).mapToObj(i -> null).collect(Collectors.toList());
Try this:
List<String> list = new ArrayList<String>(Arrays.asList(new String[100]));
for(String string : list){
System.out.println(string);
}
Well, you can write a hierarchy:
class Base<T>{
protected List<T> list;
public List<T> getList(){
return list;
}
}
class Child extends Base<String>{
public Child(){
list = new ArrayList<String>(Arrays.asList(new String[100]));
}
}
It can be used in the next way:
Base<String> base = new Child();
base.getList();
What I did was
MyClass[] array = {new MyClass(), new MyClass(), new MyClass(), new MyClass(), new ProfileSectionDTO(), new MyClass()};
List<MyClass> MyClassList = Arrays.asList(array);
Dirty, but working :)
if you want an ArrayList you can use reflection to cheat
ArrayList<T> myList = new ArrayList<T>(numEls);
Field f = ArrayList.class.getField("size");//cache this
f.setAccessible(true);
f.setInt(myList, numEls);

Categories