linkedstack5.java
来自「JAVA编程思想第四版英文原版习题答案. pdf原版的」· Java 代码 · 共 38 行
JAVA
38 行
// generics/LinkedStack5.java
// TIJ4 Chapter Generics, Exercise 5, page 626
/* Remove the type parameter on the Node class and modify the rest of the
* code in LinkedStack.java to show that an inner class has access to the
* generic type parameters of its outer class.
*/
public class LinkedStack5<T> {
// make Node a nonstatic class to access nonstatic T:
private class Node {
T item;
Node next;
Node() { item = null; next = null; }
Node(T item, Node next) {
this.item = item;
this.next = next;
}
boolean end() { return item == null && next == null; }
}
private Node top = new Node(); // End sentinel
public void push(T item) {
top = new Node(item, top);
}
public T pop() {
T result = top.item;
if(!top.end())
top = top.next;
return result;
}
public static void main(String[] args) {
LinkedStack5<String> lss = new LinkedStack5<String>();
for(String s: "Phasers on stun!".split(" "))
lss.push(s);
String s;
while((s = lss.pop()) != null)
System.out.println(s);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?