现在的位置: 首页 > 综合 > 正文

队列

2018年03月22日 ⁄ 综合 ⁄ 共 1149字 ⁄ 字号 评论关闭

队列是个先进先出的容器

下面是个简单小列子

package queue;


import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
/*
 * ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
 * @Directions 
 * @author ZhuangZi
 * @version $Id: QueueDemo.java,v 0.1 2013-9-16 下午5:20:17 ZhuangZi Exp $
 * ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
 */
public class QueueDemo {
    /***
     * @Directions 测试队列先进先出
     * @author  ZhuangZi_http://www.hzdracom.com/
     * @class queue.QueueDemo
     * @method printQ
     * @date 2013-9-16下午5:25:06
     * @param queue void
     */
    public static void printQ(Queue queue) {
        System.out.println("queue长度是:"+queue.size() );
        while (queue.peek() != null) {
            System.out.println( ">>>>>>>移除"+queue.remove() );


        }


    }
    public static void main(String[] args) {
//        向队列添加数字
//        Queue<Integer> queue = new LinkedList<Integer>();
//        Random rand = new Random(47);
//        for (int i = 0; i < 10; i++)
//            queue.offer(rand.nextInt(i + 10));
//        printQ(queue);
        //向队列添加字母元素
        Queue<Character> qc = new LinkedList<Character>();
        for (char c : "abcdefg".toCharArray()){
            qc.offer(c);
            System.out.println( "++++添加"+c); 
        }
        printQ(qc);


    }
}

抱歉!评论已关闭.