日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

未完成.队列

發布時間:2023/12/20 编程问答 19 豆豆
生活随笔 收集整理的這篇文章主要介紹了 未完成.队列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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

總結

以上是生活随笔為你收集整理的未完成.队列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。