Generic stack using array

i am not able to create generic stack because my program showing that it cannot create a generic array

actually we cannot make an array generic…try using an arraylist…if problem persists save your code on ide.codingblocks.com and share the link here

package stackProblems;

import java.util.ArrayList;

public class GenericStackUsingArrays {
protected ArrayListarr;
protected int top;

public GenericStackUsingArrays(int size) throws Exception {
	if (size == 0 || size == 1)
		throw new Exception("invalid size");

	this.arr = new ArrayList<T>(size);
	this.top = -1;
}

public void push(T data) throws Exception {
	if (this.top == arr.size() - 1) {
		throw new Exception("Stack overflow");
	}
	this.arr.set(this.top+1,data);
	this.top += 1;
}

public T pop() throws Exception {
	if (this.top == -1) {
		throw new Exception("stack is empty!!");
	}
	T rv = this.arr.get(this.top);
	this.top -= 1;
	return rv;
}

public void display() throws Exception {
	if (this.top == -1) {
		throw new Exception("stack is empty!!");

	}
	for (int i = this.top; i >= 0; i--) {
		System.out.print(this.arr.get(i) + "=>");
	}
	System.out.println("END");
}

public T top() throws Exception {
	if (this.top == -1) {
		throw new Exception("stack is empty!!");
	}
	T rv = this.arr.get(this.top);
	return rv;
}

public int size() {
	return this.top + 1;
}

}

package stackProblems;

import java.util.ArrayList;

public class DynamicStackGeneric extends GenericStackUsingArrays{

public DynamicStackGeneric(int size) throws Exception {
	super(size);
}

public  void push(T data) throws Exception {
	if (this.size() == this.arr.size()) {
		ArrayList<T> arr1 = new ArrayList<>(this.arr.size() * 2);
		for (int i = 0; i < this.size(); i++) {
			arr1.set(i,  (T) this.arr.get(i));
		}
		this.arr = arr1;
	}
	super.push(data);
}

}

please go through the generics video to see how we make a class as generic…

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.