casting a generic array in java
Posted
by
liloboy
on Stack Overflow
See other posts from Stack Overflow
or by liloboy
Published on 2011-02-27T15:04:15Z
Indexed on
2011/02/27
15:24 UTC
Read the original article
Hit count: 229
The implementation is for a linked list in java :
public AnyType[] toArr() {
AnyType[] arr = (AnyType[]) new Object[size];
int i = 0;
Node<AnyType> current = head.next;
while (cur != head){
arr[i] = current.data;// fill the array
i++;
current = current.next;
}
return arr;
}
public static void main(String[] args) {
System.out.println(ll.toArr().toString());
}
The error that I get:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
Thanks.
© Stack Overflow or respective owner