java 隊列的初學習

來源:互聯網
上載者:User

標籤: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 隊列的初學習

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.