zoukankan      html  css  js  c++  java
  • java 队列和栈的初学习

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

    栈的构造方法:
      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() 方法在操作失败的时候会抛出异常,所以不建议使用。

  • 相关阅读:
    list 、set 、map 粗浅性能对比分析
    利用 Windows Azure 实现“云优先”
    利用代码改变世界 #AzureDev
    openssl 加密
    openssl 加密
    tls和ssl
    tls和ssl
    从输入框获取输入,插入到文本框
    从输入框获取输入,插入到文本框
    windows expect-5.21r1b1-setup.exe 下载链接
  • 原文地址:https://www.cnblogs.com/wangzhaoshuang/p/7562956.html
Copyright © 2011-2022 走看看