casting a generic array in java
- by liloboy
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.