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.
Object[]norComparable[]areInteger[]s, so you'll never be able to assign one to the other. The type safety warning you get with the cast toE[]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.Classobject). This is whatArrayListdoes. You may want to look at its source code.Something<E>?