0

Here is my code:

public class MyClass<A, B> {
    ArrayList<A> arrayA;
    ArrayList<B[]> arrayB;

    ...// other methods

    public void put(A a, B b) {
        //in case 'a' does not exist in arrayA, I want to create new array in arrayB (generic array)
        B[] temp = new B[]();
        //add some item here
        arrayB.add(temp)
    }
}

This doesn't work and I don't know why. Initialization of new array B[] gives compile error. What's the way I could fix this? Or how could I go around this?

1 Answers1

0

The problem is with type erasure. Generics (ie. B) don't exist at run time, so you cannot make a new array ([]) with it

ControlAltDel
  • 28,815
  • 6
  • 42
  • 68