I have a short class:
public class Stack {
private int[] data;
private int Top;
Public Stack(int size) {
data = new int[size];
top = -1;
}
public void Push (int value) {
top++;
data[top] = value;
}
public int pop() {
return data[top--];
}
public int top() {
return data[top];
}
And i'm getting bunch of errors "cannot convert from int to T"... And also getting an error in the array definition of the constructor...
This is my code, i'm a beginner please help me to understand this:
public class Stack <T> {
private T[] data;
private T top;
Public Stack(T size) {
data = new T[size];// im getting error here "cannot create a generic array of T...
top = -1; // what should I do with this?
}
public void Push (T value) {
top++; //cannot convert from int to T
data[top] = value; //cannot convert from int to T
}
public T pop() {
return data[top--]; //cannot convert from int to T
}
public T top() {
return data[top]; //cannot convert from int to T
}