Java 實(shí)例 - 獲取鏈表的元素
以下實(shí)例演示了使用 top() 和 pop() 方法來獲取鏈表的元素:
/* author by w3cschool.cc Main.java */ import java.util.*; public class Main { private LinkedList list = new LinkedList(); public void push(Object v) { list.addFirst(v); } public Object top() { return list.getFirst(); } public Object pop() { return list.removeFirst(); } public static void main(String[] args) { Main stack = new Main(); for (int i = 30; i < 40; i++) stack.push(new Integer(i)); System.out.println(stack.top()); System.out.println(stack.pop()); System.out.println(stack.pop()); System.out.println(stack.pop()); } }
以上代碼運(yùn)行輸出結(jié)果為:
39 39 38 37
更多建議: