Stacks
import java.util.ArrayList;
public class Stack<T> {
private ArrayList<T> stack;
public Stack() {
stack = new ArrayList<>();
}
public void push(T item) {
stack.add(item);
}
public T pop() {
return stack.remove(stack.size() - 1);
}
public boolean isEmpty() {
return stack.isEmpty();
}
public int size() {
return stack.size();
}
public String toString(){
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
for (int i = 0; i < stack.size(); i++) {
stringBuilder.append(stack.get(i));
if (i != stack.size() - 1) {
stringBuilder.append(", ");
}
}
stringBuilder.append("]");
return stringBuilder.toString();
}
}
class Main{
public static void main(String[] args){
Stack<Integer> Stack = new Stack();
Stack.push(3);
Stack.push(4);
Stack.push(5);
System.out.println(Stack);
System.out.println(Stack.size());
System.out.println("popped this: " + Stack.pop());
System.out.println(Stack);
}
}
Main.main(null);