I'm creating an array of ArrayLists using the following code:
ArrayList<Integer>[] list = new ArrayList[n];
As it is said that array of generics is not allowed. How is that my code compiles?
Because you are initializing an array not the ArrayList Look at this:
ElementType [] name = new ElementType[size];
Here your element type is ArrayList
Look at this site:Array of ArrayList
refer java documentation here. You cannot create arrays of parameterized types. if it were allowed you can then create generic lists which each element of a different type. That is just not allowed. The documentation explains this well
ArrayList<Integer>[] list = new ArrayList[n];new ArrayList[n]is using the raw type ofArrayList. The compiler should be emitting a warning saying that code is unsafe/unchecked. It's similar to casting a raw type to a generic type—it's allowed but isn't safe.