Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

generic array creation

843793Jun 15 2004 — edited Apr 18 2006
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 ?
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 16 2006
Added on Jun 15 2004
15 comments
1,293 views