0

edit: I don't see how the other question answers mine. I already have an explicit cast (E[]) which the author of the other question doesn't have. I also tried something like seq1 = (Integer[]) iw.result(); but that doesn't work either.

I have the following code:

public class Something<E extends Comparable<E>> {
E[] seq;
public Something() {
    seq = (E[]) new Object[100];
}

public E[] result() {
    return sequence;
}

public static void main(String[] args) {
    Something<Integer> iw = new Something<Integer>();
    Integer[] seq1 = new Integer[100];
    seq1 = iw.result();
}}

I get an errormessage in the way of: [Object can't be cast to [Comparable

So I change the Something-Constructor to:

seq = (E[]) new Comparable[100];

Now I get an errormessage in the way of: [Comparable can't be cast to [Integer

Is there any way to make the above code work? I know I would be better off working with Collections here, but I'm just curious what's wrong with my code.

woztt
  • 11
  • 1
  • Well, obviously, neither `Object[]` nor `Comparable[]` are `Integer[]`s, so you'll never be able to assign one to the other. The type safety warning you get with the cast to `E[]` should hint at that problem. You can't create a array based on the generic parameter is what the duplicate is trying to tell you. You can use the the elements in the array as if they were generic but you'll never be able to use the array as an array of the concrete type argument. – Sotirios Delimanolis Mar 16 '15 at 14:43
  • [This answer](http://stackoverflow.com/a/4221845/438154) proposes a solution if you already have an array of the type you want (and its corresponding `Class` object). This is what `ArrayList` does. You may want to look at its source code. – Sotirios Delimanolis Mar 16 '15 at 14:44
  • I found a better duplicate for you. – Sotirios Delimanolis Mar 16 '15 at 14:46
  • @SotiriosDelimanolis: I'm not so sure I'm satisfied with that as a dupe, either. The whole thing waxes philosophical about what it means to create generic arrays, but I don't feel like it addresses this concern directly. – Makoto Mar 16 '15 at 14:58
  • Why not just `Something`? – Warkst Mar 16 '15 at 15:01
  • @Warkst: That loses the desired bound to `Comparable`. While I don't know the *rationale* for stuffing a bunch of comparable objects into an array, it's still very much desired. – Makoto Mar 16 '15 at 15:05
  • hmmm can't seq=new E[100] work ? – niceman Mar 16 '15 at 17:44
  • @niceman: [**No.**](http://stackoverflow.com/q/529085/1079354) – Makoto Mar 16 '15 at 17:50

1 Answers1

2

A couple of issues here.

First, that's not an unbounded generic parameter, that's got a bound of Comparable<E> attached to it.

During type erasure, with your class declared as:

public class Something<E extends Comparable<E>> {
    E[] seq;
}

...E is bound to Comparable.

public class Something {
    Comparable[] seq;
}

This is why your casting isn't going to work, since an Object is not a Comparable. You would want to use new Comparable instead.

public class Something<E extends Comparable<E>> {
    E[] seq;

    public Something() {
        seq = (E[]) new Comparable[100];
    }
}

Now, Java is and should break on the last two statements.

Integer[] seq1 = new Integer[100];
seq1 = iw.result();

iw.result() is bound to Comparable[], not Integer[]. A Comparable[] can never become an Integer[].

You can do this instead to eschew the ClassCastException instead:

Comparable[] seq1 = new Integer[100];

This will work since an Integer is Comparable. This is due to the fact that arrays are covariant (that is to say, an Integer[] is a subtype of a Comparable[]).

Makoto
  • 96,408
  • 24
  • 164
  • 210
  • I hate that you've done this. Your answer doesn't provide anything new. The [question linked](http://stackoverflow.com/questions/17831896/creating-generic-array-in-java-via-unchecked-type-cast) and its answers already explain all of this. – Sotirios Delimanolis Mar 16 '15 at 15:10
  • It didn't feel like it explained it clearly enough to me, so that's why I did it. I'm sorry that you don't agree with it, but I'm not entirely remorseful that I've answered the question in this way. – Makoto Mar 16 '15 at 15:11