http://stackoverflow.com/questions/18581002/how-to-create-a-generic-array
/**
* array of generic elements to represent the stack
*/
private T[] stack;
/**
* Creates an empty stack using the specified capacity.
* @param initialCapacity represents the specified capacity
*/
public ArrayStack (int initialCapacity)
{
top = 0;
stack = (T[])(new Object[initialCapacity]);
}
/**
* array of generic elements to represent the stack
*/
private T[] stack;
/**
* Creates an empty stack using the specified capacity.
* @param initialCapacity represents the specified capacity
*/
public ArrayStack (int initialCapacity)
{
top = 0;
stack = (T[])(new Object[initialCapacity]);
}
Generics supposed to add an abstraction level of the signature of the application. Not to instantiate or cast objects.
ReplyDeleteAgree. Ideally, I wish java will support code like below,
ReplyDeletestack = new T[initialCapacity];