当前位置: 首页 > news >正文

未完成.队列

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-----*/

 

将编程看作是一门艺术,而不单单是个技术。 敲打的英文字符是我的黑白琴键, 思维图纸画出的是我编写的五线谱。 当美妙的华章响起,现实通往二进制的大门即将被打开。

相关文章:

  • vuex的使用
  • 21、PHP和python/JAVA比,哪个更好学?
  • word 2007每次打开总出现setup error
  • C# WinForm开发系列 - GDI+【转】
  • 【自用】 Kotlin 创建自己的对话框
  • 从antlr扯淡到一点点编译原理
  • 用Canvas画一棵二叉树
  • webpack+react环境搭建与hello world
  • iOS 实现UINavigation全屏滑动返回(一)
  • c++那些事儿4 0 多态
  • 1.Node.js
  • 使用Doxygen生成C#帮助文档
  • [喵咪大数据]Hadoop集群模式
  • The working copy is locked due to previous error
  • iOS推送Tips
  • ES6指北【2】—— 箭头函数
  • [分享]iOS开发 - 实现UITableView Plain SectionView和table不停留一起滑动
  • 〔开发系列〕一次关于小程序开发的深度总结
  • 07.Android之多媒体问题
  • C++类的相互关联
  • Github访问慢解决办法
  • IDEA常用插件整理
  • JAVA之继承和多态
  • JDK 6和JDK 7中的substring()方法
  • Js基础——数据类型之Null和Undefined
  • Linux中的硬链接与软链接
  • python大佬养成计划----difflib模块
  • webpack+react项目初体验——记录我的webpack环境配置
  • 面试总结JavaScript篇
  • 前端攻城师
  • 前端面试题总结
  • 前嗅ForeSpider教程:创建模板
  • 软件开发学习的5大技巧,你知道吗?
  • 设计模式 开闭原则
  • 双管齐下,VMware的容器新战略
  • 写给高年级小学生看的《Bash 指南》
  • 转载:[译] 内容加速黑科技趣谈
  • ​软考-高级-系统架构设计师教程(清华第2版)【第20章 系统架构设计师论文写作要点(P717~728)-思维导图】​
  • #HarmonyOS:软件安装window和mac预览Hello World
  • #大学#套接字
  • (2)(2.4) TerraRanger Tower/Tower EVO(360度)
  • (2.2w字)前端单元测试之Jest详解篇
  • (Redis使用系列) SpringBoot中Redis的RedisConfig 二
  • (官网安装) 基于CentOS 7安装MangoDB和MangoDB Shell
  • (简单) HDU 2612 Find a way,BFS。
  • (每日持续更新)信息系统项目管理(第四版)(高级项目管理)考试重点整理 第13章 项目资源管理(七)
  • (四)【Jmeter】 JMeter的界面布局与组件概述
  • (转)linux 命令大全
  • .mkp勒索病毒解密方法|勒索病毒解决|勒索病毒恢复|数据库修复
  • .NET 8.0 中有哪些新的变化?
  • .NET Core 通过 Ef Core 操作 Mysql
  • .Net Core 中间件验签
  • .NET Core中的去虚
  • .net web项目 调用webService
  • .NET 回调、接口回调、 委托