0

Please consider the following code:

public class A<T> {  
    public void main(String[] args) {
        A<String>[] array = new A<String>[10];
        System.out.println(array.length);
    }
}

I am trying to define (and allocate space for it) an array of 10 elements where each element of the array is of type A. I thought the above code would do it, but it fails to compile.

Thanks, Bob

Bob
  • 240
  • 2
  • 8
  • 1
    [Take a look how `ArrayList` does it](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/ArrayList.java) . Baseline: generics and arrays do not mix well – Turing85 May 16 '17 at 21:25
  • you can't create an array of a class with generic type. You could do it with raw types, but that is something you should avoid. – dumbPotato21 May 16 '17 at 21:25
  • Type Erasure makes it so that you can't do this. Simply define your array as `A[]`. – nasukkin May 16 '17 at 21:26

0 Answers0