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.
Comparablebut you cast it toE.EandComparableare not the same, so this makes no sense.CCArrayrepresents anE[][]. So why not use this directly? Furthermore why not use Collections? And no, there are no real reasons for using Generics as arrays.TreeSet? It has maxO(log n)in all operations and is sorted.