Generics Array


Java does not permit the construction of generic array. Suppose, for example, that we want to design a class that has an array field
public class Table<AnyType>
{
private AnyType[] contents = new AnyType[5];
...
}
It's hard to understand, but this is illegal! A workaround is to construct an array of objects with a type-cast:

public class Table<AnyType>
{
private AnyType[] stack = (AnyType[]) new Object[5];
...
}
source  -
http://www.cs.cmu.edu/~adamchik/15-121/lectures/Collections/collections.html

Comments

Popular posts from this blog

Palindrome program in Java using BufferedReader

OOPS: Decomposition and Abstraction