Untitled
unknown
plain_text
3 years ago
1.3 kB
9
Indexable
import java.util.Arrays; public class Test { public static void main(String[] args) { GenericStack<Integer> numberStack = new GenericStack<>(); for (int i = 0; i < 10000; i++) { numberStack.push(i); } System.out.println("Current size: " + numberStack.getSize()); System.out.println("Is stack empty? " + numberStack.isEmpty()); System.out.println("Pop: " + numberStack.pop()); System.out.println("Push: " + numberStack.push(1)); System.out.println("Current Size " + numberStack.getSize()); } static private class GenericStack<E> { private int capacity = 100; private int size; private E[] array = (E[])new Object[capacity]; public int getSize() { return size; } public void push(E o) { array[size++] = o; if (size == capacity) { E[] tmp = (E[])new Object[capacity *= 2]; System.arraycopy(array, 0, tmp, 0, array.length); array = tmp; } } public E pop() { if (size == 0) return null; else return array[--size]; } public boolean isEmpty() { return size == 0; } } }
Editor is loading...