public interface Stack<E> {
...
}
Then, for an 8-element array, the following code might be natural:private E[] stack = new E[8];but this is illegal. A workaround is to construct an array of objects with a type-cast:
private E[] stack = (E[]) new Object[8];but this will generate a compiler warning about an unchecked type conversion. This is only generated, however, when the source file containing the type-cast is compiled, not when a file of application code importing this class is compiled.
There is considerable discussion of this aspect of Java 5, as a websearch for some phrase such as generic array java will show. In practice, if efficiency is important, it is not possible to use arrays to implement generic data structures without generating such warnings. In implementing the generic version of the DataStructures package, I have been willing to accept such warnings, as the Java source code will show. The implementers of the Collections framework have taken the same approach. This issue will remain opaque, of course, to any client of a pre-compiled package.