@@ -0,0 +1,2428 @@
+package Basics;%0A%0A/**%0A * Created by matao on 5/27/16.%0A */%0Apublic class Stack%3CT%3E %7B%0A    private int initialCapacity;    // %E5%88%9D%E5%A7%8B%E6%97%B6stack%E7%9A%84%E5%A4%A7%E5%B0%8F%0A    private int size;               // stack%E4%B8%AD%E5%85%83%E7%B4%A0%E6%95%B0%E9%87%8F%0A%0A    private T%5B%5D array;%0A%0A    public Stack(int initialCapacity) %7B%0A        this.initialCapacity = initialCapacity;%0A        this.size = 0;%0A%0A        array = (T%5B%5D) new Object%5BinitialCapacity%5D;%0A    %7D%0A%0A    public void push(T item) %7B%0A        if (size == array.length) %7B%0A            resize(array.length * 2);%0A        %7D%0A        array%5Bsize%5D = item;%0A        size++;%0A    %7D%0A%0A    public T pop() %7B%0A        size--;%0A        T popItem = array%5Bsize%5D;%0A        array%5Bsize%5D = null;%0A        if (size %3E 0 && size == array.length / 4) %7B%0A            resize(array.length / 2);%0A        %7D%0A        return popItem;%0A    %7D%0A%0A    public void resize(int newCapacity) %7B%0A        T%5B%5D tmp = (T%5B%5D) new Object%5BnewCapacity%5D;%0A        for (int i = 0; i %3C size; i++) %7B%0A            tmp%5Bi%5D = array%5Bi%5D;%0A        %7D%0A        array = tmp;%0A    %7D%0A%0A    // %E8%8E%B7%E5%8F%96stack%E4%B8%AD%E5%90%AB%E6%9C%89%E5%85%83%E7%B4%A0%E7%9A%84%E6%95%B0%E9%87%8F%0A    public int getSize() %7B%0A        return size;%0A    %7D%0A%0A    // %E8%8E%B7%E5%8F%96stack%E5%BD%93%E5%89%8D%E5%AE%B9%E9%87%8F%0A    public int getCapacity() %7B%0A        return array.length;%0A    %7D%0A%0A    public boolean isEmpty() %7B%0A        return size == 0;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Stack%3CInteger%3E stack = new Stack%3C%3E(3);%0A        System.out.println(%22size: %22 + stack.getSize());%0A        System.out.println(%22capacity: %22 + stack.getCapacity());%0A%0A        stack.push(1);%0A        stack.push(2);%0A        stack.push(3);%0A        System.out.println(%22--------------- after push 3 items ----------------------%22);%0A        System.out.println(%22size: %22 + stack.getSize());%0A        System.out.println(%22capacity: %22 + stack.getCapacity());%0A%0A        stack.push(4);%0A        System.out.println(%22--------------- after push another 1 item ----------------------%22);%0A        System.out.println(%22size: %22 + stack.getSize());%0A        System.out.println(%22capacity: %22 + stack.getCapacity());%0A%0A        stack.pop();%0A        stack.pop();%0A        System.out.println(%22--------------- after pop 2 items ----------------------%22);%0A        System.out.println(%22size: %22 + stack.getSize());%0A        System.out.println(%22capacity: %22 + stack.getCapacity());%0A%0A        stack.pop();%0A        System.out.println(%22--------------- after pop another 1 item ----------------------%22);%0A        System.out.println(%22size: %22 + stack.getSize());%0A        System.out.println(%22capacity: %22 + stack.getCapacity());%0A    %7D%0A%7D%0A

Add stack