码迷,mamicode.com
首页 > 编程语言 > 详细

java 队列的初学习

时间:2017-09-20 19:38:05      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:insert   lan   队列   class   lang   lin   eth   exception   specified   

栈是先进后出的一个线性表,允许在表的顶部添加元素和在表的顶部删除元素。

栈的构造方法:
  public stack() {}

  push() : 在表的顶部添加元素和 addElement() 是一样的。

  pop() : 在表的顶部获取并移除第一个元素

  peek() : 在表的顶部获取第一个元素但是不移除

  

 

 

队列是一个特殊的线性表,只允许在表的后端添加元素,在表的前端删除元素。

LinkedList类实现了Queue,所以可以将LinkedList当做队列来使用。

offer() : 在表的后端添加一个元素。

poll() : 在表的前端获取并删除第一个元素。

peek() : 在表的前端获取第一个元素。

element() : 在表的前端获取第一个元素。

 

add():Inserts the specified element at the tail of this queue. As the queue is unbounded, this method will never throw IllegalStateException or return false.

offer():Inserts the specified element at the tail of this queue. As the queue is unbounded, this method will never return false.

区别:两者都是往队列尾部插入元素,不同的时候,当超出队列界限的时候,add()方法是抛出异常让你处理,而offer()方法是直接返回false

 

add() 和 remove() 方法在操作失败的时候会抛出异常,所以不建议使用。

 

java 队列的初学习

标签:insert   lan   队列   class   lang   lin   eth   exception   specified   

原文地址:http://www.cnblogs.com/wangzhaoshuang/p/7562956.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!