0

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.

  • you create an array of `Comparable` but you cast it to `E`. `E` and `Comparable` are not the same, so this makes no sense. – Absurd-Mind May 03 '14 at 14:34
  • I agree that my approach was slightly nonsensical, but I was grasping at straws. I have successfully mad single dimensional structures like this using arrays and generics, so for me it stood to reason that I should be able to make a multidimensional structure of the like. – Mikeologist May 03 '14 at 15:51
  • Your `CCArray` represents an `E[][]`. So why not use this directly? Furthermore why not use Collections? And no, there are no real reasons for using Generics as arrays. – Absurd-Mind May 03 '14 at 15:56
  • Why not use collections? Speed and overhead. I am striving to make a super array that has at worst O(log(n)) efficiency for any operation and is not limited by any size restriction. With the help that I received here, I have done just that. – Mikeologist May 03 '14 at 16:03
  • What do you mean by size restriction? Since you are using arrays there is a size restriction! What about `TreeSet`? It has max `O(log n)` in all operations and is sorted. – Absurd-Mind May 03 '14 at 16:15

3 Answers3

3

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.

Absurd-Mind
  • 7,884
  • 5
  • 35
  • 47
  • Your answer was correct; thank you. However, I think it is important to be able to mix generics and arrays as I hate wasted space and linear resources. – Mikeologist May 03 '14 at 15:48
2

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 :)

Cartier
  • 429
  • 4
  • 15
  • It helped looking at it that way, but ultimately a list for the application of this structure is bad. It would work, but it would defeat the efficiency of the structure; which is the ultimate goal for me. Thank you for the ideas though. – Mikeologist May 03 '14 at 15:56
  • No problem, just want to help :) – Cartier May 04 '14 at 01:54
0

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] ?

Nicolas Defranoux
  • 2,646
  • 1
  • 10
  • 13