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.

8
  • 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. Commented Mar 16, 2015 at 14:43
  • This answer 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. Commented Mar 16, 2015 at 14:44
  • I found a better duplicate for you. Commented Mar 16, 2015 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. Commented Mar 16, 2015 at 14:58
  • Why not just Something<E>? Commented Mar 16, 2015 at 15:01

1 Answer 1

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[]).

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

2 Comments

I hate that you've done this. Your answer doesn't provide anything new. The question linked and its answers already explain all of this.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.