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);
    		 
    	}
    }
    

      

  • 相关阅读:
    Android的目录结构说明
    IOS-线程(GCD)
    iOS UI-线程(NSThread)及其安全隐患与通信
    iOS UI-自动布局(AutoLayout)
    iOS UI-自动布局(Autoresizing)
    IOS-Core Data的使用
    OC 数据持久化(数据本地化)- 本地存储
    iOS UI-应用管理(使用Cell模板)
    IOS UI-QQ好友列表
    IOS-多线程
  • 原文地址:https://www.cnblogs.com/xyk1987/p/8287846.html
Copyright © 2011-2022 走看看