I'm learning to work with generics, I can't understand why this is throwing an exception:
My 'GenericList' class:
package com.company.generics;
public class GenericList<T extends Number>{
private T[] items = (T[]) new Object[10];
private int count;
public void add(T item){
items[count++] = item;
}
}
My main class:
package com.company;
import com.company.generics.GenericList;
public class Main {
public static void main(String[] args) {
GenericList<Integer> g = new GenericList<>();
}
}
Exception thrown:
Exception in thread "main" java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.Number; ([Ljava.lang.Object; and [Ljava.lang.Number; are in module java.base of loader 'bootstrap')
at com.company.generics.GenericList.<init>(GenericList.java:4)
at com.company.Main.main(Main.java:9)