Java筆記集合之棧和隊列

棧和隊列

(1)Stack繼承了Vector
(2)LinkedList實現了Queue接口

package com.jlz;

import java.util.*;

/**
 * 
 * @author Jlzlight Stack Queue
 */
public class TestStack {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Stack<Object> stack = new Stack<Object>();
		// 入棧
		stack.push("C");
		stack.push("B");
		stack.push("A");
		// 獲取棧頂元素
		System.out.println(stack.peek());
		// 出棧
		System.out.println(stack.pop());
		System.out.println(stack.pop());
		System.out.println(stack.pop());

		// LinkedList實現Queue接口
		Queue<Object> queue = new LinkedList<Object>();
		// 入隊列
		queue.add("A");
		queue.add("B");
		queue.add("C");
		// 隊首元素
		System.out.println(queue.peek());
		// 出隊列
		System.out.println(queue.poll());
		System.out.println(queue.poll());
		System.out.println(queue.poll());
	}

}


發佈了39 篇原創文章 · 獲贊 16 · 訪問量 9274
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章