← Back to Question List
Java Code Walkthrough #40
Generic Stack Push and Pop
📄 Code
Read carefully — what does this print?1import java.util.ArrayList;23class Stack<T> {4 private ArrayList<T> items = new ArrayList<>();56 public void push(T item) {7 items.add(item);8 }910 public T pop() {11 return items.remove(items.size() - 1);12 }1314 public int size() {15 return items.size();16 }17}1819public class Main {20 public static void main(String[] args) {21 Stack<String> stack = new Stack<>();22 stack.push("a");23 stack.push("b");24 stack.push("c");25 System.out.println(stack.pop());26 System.out.println(stack.size());27 }28}