zoukankan      html  css  js  c++  java
  • java.util.Queue是队列的接口,其继承自Collection

    package collection;
    
    import java.util.LinkedList;
    import java.util.Queue;
    /**
     * 队列
     * 队列是常见的数据结构,可以存放一组元素,但是存取
     * 元素必须遵循先进先出的原则。
     * java.util.Queue是队列的接口,其继承自Collection
     * 常用实现类:LinkedList
     * @author 清风已来
     *
     */
    
    public class QueueDemo {
    	public static void main(String[] args) {
    		 Queue<String> queue =new LinkedList<String>();
    		 /*
    		  * boolean offer(E e)
    		  * 入队操作,将改定元素添加到队列末尾
    		  */
    		 queue.offer("one");
    		 queue.offer("two");
    		 queue.offer("three");
    		 queue.offer("four");
    		 
    		 System.out.println(queue);
    		 /*
    		  * E poll()
    		  * 出队操作,获取队首元素,获取后该元素即重队列中被删除
    		  */
    		 String str =queue.poll();
    		 System.out.println(str);
    		 System.out.println(queue);
    		 /*
    		  *  peek()
    		  *  引用队首元素,获取队首元素但是不会将其从队列中移除
    		  */
    		 str =queue.peek();
    		 System.out.println(str);
    		 System.out.println(queue);
    		 /*
    		  *遍历队列
    		 *1.迭代器模式
    		  * 
    		  */
    		 for(String s :queue) {
    			 System.out.println(s);
    		 }
    		 System.out.println(queue);
    		 /*2:自行遍历
    		  * 用poll方法
    		  * 
    		  */
    		 
    		 while(queue.size()>0) {
    			 String e =queue.poll();
    		 }
    		 System.out.println(queue);
    		 
    		 for(int i =queue.size();i>0;i--) {
    			 String a =queue.poll();
    		 }
    		 System.out.println(queue);
    		 
    	}
    }
    

      

  • 相关阅读:
    nginx面试题
    解决zabbix图形显示“方块”问题
    echo命令详解
    Ubantu1804更换阿里源
    arp命令
    /boot、/和/swap分区扩容
    windows server 2016 安装 .Net Framework失败解决方案
    报错kernel:NMI watchdog: BUG: soft lockup
    jenkins 常用插件源URL
    jenkins rpm包方式安装
  • 原文地址:https://www.cnblogs.com/xyk1987/p/8287846.html
Copyright © 2011-2022 走看看