Question

I am having trouble in creating an array to contain my custom object arrays.

I want to create the container as a standard array of comparable arrays for multiple reasons:

  • Every custom object array contained within must be a different length
  • The custom object array has automatic sorting tools built in, that are not appropriate for the larger containing array into which I want them to fit

The custom object array works and has been thoroughly tested.

The exception I receive durring the creation of the larger object is:

Exception in thread "main" java.lang.ClassCastException:

[Ljava.lang.Comparable; cannot be cast to [LSortedFixedCircularArray;

Here is the applicable code

//From SortedFixedCircularArray.java
public class SortedFixedCircularArray<E extends Comparable<E>> 
{
    SortedFixedCircularArray(int fixed_capacity_in)
    {
        data = newArray(fixed_capacity_in);
        front = 0;
        rear = 0;
        size = 0;
        capacity = data.length;
    }

    @SuppressWarnings("unchecked")
    private E[] newArray(int size)
    {
        return (E[]) new Comparable[size];
    }
}

//From CCArray.java
public class CCArray <E extends Comparable<E>>
{
    private SortedFixedCircularArray<E>[] data;
    private long size;
    private long capacity;
    private final static long DEFAULT_CAPACITY = 15;

    CCArray()
    {
        this(DEFAULT_CAPACITY);
    }
    @SuppressWarnings("unchecked")
    CCArray(long initial_capacity)
    {
        int height = getIndices(initial_capacity)[0]+1;
        height *= 2;

            //!THIS IS THE PROBLEM LINE!
        data = (SortedFixedCircularArray<E>[]) new Comparable[height];  //Unchecked Casting

        int temp = (height/2)-1;
        patternInit(temp);
        capacity = initial_capacity;
        size = 0;
    }
    private void patternInit(int height_in)
    {
        //initialize the individual arrays here
    }
    private int[] getIndices(long index)
    {
        //Return int[] with i&j indices
    }
}

I hypothesize that java sees this Object array as an Object and not an Object array. I think this is a type of jagged-array, but as stated above it cannot be composed of the same type of array as that will cause other issues. Please help.

Was it helpful?

Solution

just change your problematic line to this:

// Unchecked Casting
data = (SortedFixedCircularArray<E>[]) new SortedFixedCircularArray[height];

The cast is optional, this works also:

data = new SortedFixedCircularArray[height];

Basically it is a bad idea to mix Generics and Arrays.

OTHER TIPS

You could try using a generic list to index all of your objects, and then just using a for-each loop to call and test them, however I don't know if that is desirable for you.
I am fairly certain it is because you have not derived the Comparable class from your SortedFixedCircularArray, and in turn causes you to be unable to cast an object of that type and then reference it to a Comparable
You could try this:

`class SortedFixedCircularArray<E> extends Comparable<E> {
     //In Which This Should Now Work:
     data = (SortedFixedCircularArray<E>[]) new Comparable[height];
}  
`

The problem could also be that you are referencing a comparable as an array instead of an <>, which would also result in a cast exception.

I Hope This Helps :)

You cannot cast a Comparable object to a SortedFixedArray, while you could cast a SortedFixedArray to a Comparable. So you could use:

data = new SortedFixedCircularArray<E>[height];

You will have the same problem for

private E[] newArray(int size) {
  return (E[]) new Comparable[size];
}

Why don't you just do new E[size] ?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top