This is based off my last question Why am i getting a class cast exception(with generics, comparable)?
Here is my design again. I have an abstract super class, AbstractArrayList, and two concrete subclasses that extend it, sorted and unsorted array list.
Here's AbstractArrayList which manages the actual data because it needs to for the already implemented methods.
public abstract class AbstractArrayMyList<E> implements MyList<E> {
protected E[] elementData;
.....
}
Here is the declaration for ArrayListSorted, which extends AbstractArrayMyList
public class ArrayListSorted<E extends Comparable<E>> extends AbstractArrayMyList<E>
And the lines of test code that caused the exception
ArrayListSorted<Integer> toTestInteger = new ArrayListSorted<Integer>()
toTestInteger.insert(0);
assertEquals(toTestInteger.get(0).intValue(), 0);
And the actual exception itself
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Comparable;
at myarraylist.ArrayListSorted.getIndex(ArrayListSorted.java:38)
ArrayListSorted - java line 38 is inside this method
public int getIndex(E value) {
....
line 38 - if (value.compareTo(elementData[mid]) < 0) hi = mid - 1;
The response I got from the question clarified what the exception was caused by. When I tried making this call
value.compareTo(elementData[mid]) < 0
value was of the correct type because the extends part will narrow the type object to Comparable type. However the JVM which runs the java bytecode, will only recognize the elementData array as an object array so when I am trying to cast elementData[mid] to Comparable, it's not actually of Comparable type.
The solution that chrylis gave me in the other question was to have a constructor in the AbstractArrayList that will construct the right typed array
protected AbstractArrayMyList(Class<E> clazz) {
this.elementClass = clazz;
this.elementData = Array.newInstance(clazz, INITIAL_SIZE);
}
I am trying to call this constructor in my sorted array list subclass, with
public class ArrayListSorted<E extends Comparable<E>> extends AbstractArrayMyList<E>
public ArrayListSorted() {
super(Class.forName(("E"));
}
...
Syntax I got from this thread - Passing a class as an argument to a method in java
However I get a compiler error - The constructor AbstractArrayMyList(Class) is undefined. Does anyone know what the issue is? I defined that same constructor that chrylis provided me in my question in AbstractArrayList that takes in a class instance.