未完成.队列
package 隊(duì)列queue;import java.util.LinkedList;
import java.util.NoSuchElementException;
import java.util.Queue;/***
* 創(chuàng)建時(shí)間:2017年12月8日 下午3:47:25
* 項(xiàng)目名稱:集合
* @author ukyozq
* @version 1.0
* @since JDK 9.0
* 文件名稱:Queue_Test01.java
* -----------------------------------------------------------------~
* 類說明:以下代碼顯示如何將鏈表用作 FIFO[*]隊(duì)列.
* [*] First Input First Output 的縮寫,先入先出隊(duì)列,
* 這是一種傳統(tǒng)的按序執(zhí)行方法,先進(jìn)入的指令先完成并引退,跟著才執(zhí)行第二條指令
-------------------------------------------------^_^---------------*/
public class Queue_Test01
{public static void main(String[] args){/*------------------------------------------------~LinkedList 和 PriorityQueue 是 Queue 接口的兩個(gè)實(shí)現(xiàn)類. LinkedList 還實(shí)現(xiàn)了 List 接口 --------------------------------------------------*/Queue<String> queue = new LinkedList<>();/*--------------------------------------------------~add() 如果可能,向隊(duì)列中添加一個(gè)元素.否則,它拋出異常. offer() will work the same as add() offer() 如果不能添加元素,則將元素添加到隊(duì)列中,而不拋出異常. 它在失敗時(shí)返回false,成功時(shí)返回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 | 讓我們 刪除 元素們 直到 那 隊(duì)列 為 空 | -----------------------------------------------*//*---------------------------------------------~[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中用于判斷某種容器是否有元素的系統(tǒng)庫函數(shù)。 如用來判斷ArrayList,HashSet,HashMap是否有元素等。 peek() 返回隊(duì)列頂部,如果隊(duì)列為空而不是拋出異常,則返回null。 poll() 移除并返問隊(duì)列頭部的元素 。隊(duì)列為空不拋出異常,返回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-----*/
?
轉(zhuǎn)載于:https://www.cnblogs.com/ukzq/p/8008079.html
總結(jié)
- 上一篇: Oracle数据库定时备份脚本
- 下一篇: eclipse 国际化 $NON-NLS