Stack类继承与Vector,而Vector继承此List,Stack表示栈,栈是个算法,表示后进先出,现在写个例子玩玩:
public class StackDemo1 { public static void main(String[] args) { Stack<String> stack = new Stack<String>(); stack.add("a"); stack.add("b"); stack.add("c"); stack.add("d"); } }
public class StackDemo1 { public static void main(String[] args) { Stack<String> stack = new Stack<String>(); stack.push("a"); stack.push("b"); stack.push("c"); stack.push("d"); } }其实它底层实现都是一样的,底层实现代码:
public synchronized boolean add(E e) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = e; return true; }
原文:http://blog.csdn.net/coderinchina/article/details/41079355