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.

5
  • you create an array of Comparable but you cast it to E. E and Comparable are not the same, so this makes no sense. Commented May 3, 2014 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. Commented May 3, 2014 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. Commented May 3, 2014 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. Commented May 3, 2014 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. Commented May 3, 2014 at 16:15

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

2 Comments

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.
No problem, just want to help :)
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] ?

2 Comments

You cannot create an array of generics directly, you must create and array of a type that your generic is derived from and cast it as an array of generics. But I thank you for trying.
This is why E[] doesn't work: <stackoverflow.com/questions/14917375/…>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.