Unfortunately, you can't create a generic array. This is due to the way generics are implemented in Java, which is through type erasure. In effect, all generic types are "erased" to their bounding type (usually Object) before compilation, so all the compiler sees are Object instead of T or E or O. The erasure process generates automatic casts to ensure that the program would still work as intended.
This means that you can't do:
new O() (this gets erased to new Object(), which the compiler has no idea what to do with)
new O[] (this gets erased to new Object(), which again isn't helpful to the compiler)
What you can do is casting:
array = (O[]) new Object[size];
And in fact, that's how it's done in Java's Collections framework. You'll get an "unchecked" warning, as the compiler can't prove that this conversion is safe, but there really is no other option.
Also, few things I want to point out about your question, that you may or may not know already:
- You can't use primitives as type parameters
Object has no type parameters
newArray<O>[] = new newArray<O>[newArraySize]; should really be array = new newArray<O>[newArraySize];. You already declared the array you wanted to use. So use it!
Looks like you're implementing your own ArrayList, in fact. If you are, good luck! If you aren't, you should really be using the existing implementation, unless you need some special behavior that you can't get otherwise...