未完成.队列
package 隊列queue;import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;/***
* 創建時間:2017年12月8日 下午3:47:25
* 項目名稱:集合
* @author ukyozq
* @version 1.0
* @since JDK 9.0
* 文件名稱:Queue_Test01.java
* -----------------------------------------------------------------~
* 類說明:以下代碼顯示如何將鏈表用作 FIFO[*]隊列.
* [*] First Input First Output 的縮寫,先入先出隊列,
* 這是一種傳統的按序執行方法,先進入的指令先完成并引退,跟著才執行第二條指令
-------------------------------------------------^_^---------------*/
public class Queue_Test01
{public static void main(String[] args){/*------------------------------------------------~LinkedList 和 PriorityQueue 是 Queue 接口的兩個實現類. LinkedList 還實現了 List 接口 --------------------------------------------------*/Queue<String> queue = new LinkedList<>();/*--------------------------------------------------~add() 如果可能,向隊列中添加一個元素.否則,它拋出異常. offer() will work the same as add() offer() 如果不能添加元素,則將元素添加到隊列中,而不拋出異常. 它在失敗時返回false,成功時返回true. ----------------------------------------------------*/queue.add("Apple");queue.offer("Select");queue.offer("Canada");queue.offer("X-man");System.out.println("Queue:"+queue);/*---------------------------------------------~Let's remove elements until the queue is empty | 讓我們 刪除 元素們 直到 那 隊列 為 空 | -----------------------------------------------*//*---------------------------------------------~[queue] ↓peek() |[10][9][8][7][6][5][4][3][2][1][0] |-> -> -> -> -> -> -> -> -> -> -> -> -> -> -> ->|-----------------------------------------------*/while(queue.peek() != null){System.out.println("Head Element:"+queue.peek());queue.remove();System.out.println("Removed one element from Queue.");System.out.println("Queue:"+queue);}/*----------------------------------------------------------~IsEmpty()是Java中用于判斷某種容器是否有元素的系統庫函數。 如用來判斷ArrayList,HashSet,HashMap是否有元素等。 peek() 返回隊列頂部,如果隊列為空而不是拋出異常,則返回null。 poll() 移除并返問隊列頭部的元素 。隊列為空不拋出異常,返回null。 ------------------------------------------------------------*/System.out.println("queue.isEmpty():"+queue.isEmpty());System.out.println("queue.peek():"+queue.peek());System.out.println("queue.poll():"+queue.poll());try{String str = queue.element();System.out.println("queue.element():"+str);str = queue.remove();System.out.println("queue.remove():"+str);} catch (NoSuchElementException e){
// e.printStackTrace();System.out.println("queue.remove(): Queue is empty.");}}}/*------------------------------------~~~~
輸出:
Queue:[Apple, Select, Canada, X-man]
Head Element:Apple
Removed one element from Queue.
Queue:[Select, Canada, X-man]
Head Element:Select
Removed one element from Queue.
Queue:[Canada, X-man]
Head Element:Canada
Removed one element from Queue.
Queue:[X-man]
Head Element:X-man
Removed one element from Queue.
Queue:[]
queue.isEmpty():true
queue.peek():null
queue.poll():null
queue.remove(): Queue is empty.----------------java---QQ群:215200319-----*/
?
轉載于:https://www.cnblogs.com/ukzq/p/8008079.html
總結
- 上一篇: Oracle数据库定时备份脚本
- 下一篇: eclipse 国际化 $NON-NLS