覺得作者寫得太好了
對這個例子的理解
//類型參數不能用基本類型
//每次放新數據都成為新的top
//末端哨兵既是默認構造器創建出的符合end()返回true的節點
//: generics/LinkedStack
// A stack implemented with an internal linked structure
package generics; public class LinkedStack<T> {
private static class Node<U> {
U item;
Node<U> next;
Node() { item = null; next = null; }
Node(U item
this
this
}
boolean end() { return item == null && next == null; }
}
private Node<T> top = new Node<T>(); // End sentinel
public void push(T item) {
top = new Node<T>(item
}
public T pop() {
T result = top
if(!top
top = top
return result;
}
public static void main(String[] args) {
LinkedStack<String> lss = new LinkedStack<String>();
for(String s : "Phasers on stun!"
lss
String ss;
while((ss = lss
System
//
LinkedStack<Integer> lii = new LinkedStack<Integer>();
for(Integer i =
lii
}
Integer end;
while((end = lii
System
//
}
}
/* Output:
stun!
on
Phasers
*/
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20487.html