I was trying to create a new data type with generics but the javac compiler keeps telling me
"Note: MyStack.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
, when I try to recompile it with the option -Xlint,
"MyStack.java:8: warning: [unchecked] unchecked cast
data = (T[]) new Object[5];
^
required: T[]
found: Object[]
where T is a type-variable:
T extends Object declared in class MyStack
1 warning
. What should I do to avoid this warning?
public class MyStack<T> {
int index;
private T[] data;
public MyStack() {
index = 0;
data = (T[]) new Object[5];
}
public void push(T input) {
data[index++] = input;
}
public T pop() {
return data[--index];
}
}