Hi,
I have a problem with generic array creation.
When I try to compile the following example (see below). I always have the following error:
javac Stack.java
Stack.java:2: generic array creation
T[] pile = new T[255];
^
1 error
The code:
public class Stack<T> {
T[] pile = new T[255];
int idx = 0;
public void push(T t) {
pile[idx] = t;
idx++;
}
public T pop() {
if (idx <= 0) {
throw new RuntimeException();
}
idx--;
return pile[idx+1];
}
public static void main(String[] args) {
Stack<Element> s = new Stack<Element>();
s.push(new Element("Hello"));
System.out.println(s.pop());
}
}
What am I doing wrong ?