• 编程随笔-Java | 04.栈Stack、队列Queue和双端队列Deque


    1.Stack - 栈

    定义

    Stack<Integer> st = new Stack<>();
    
    • 1

    泛型类型必须被指定为引用数据类型。

    API

    API功能概述备注
    void push(T value)插入新的元素支持向上转型,因此value可以是基本数据类型
    T pop( )弹出栈顶元素
    T peek( )查看栈顶元素(不移除)不会移除该元素
    boolean isEmpty( )判断栈是否为空
    int size( )返回栈的元素个数
    T[] toArray(T[] arrObj)将栈转换成数组传入的参数是一个引用数据类型的数组对象
    举例:Integer[] arr = st.toArray(new Integer[0])

    示例代码

    public static void testStack(){
        // 泛型必须指定为引用数据类型
        Stack<Integer> st  = new Stack<>();
    
        // 01.入栈
        st.push(1);
        st.push(2);
        st.push(3);
    
        // 02.出栈
        int popResult = st.pop();
        System.out.println(popResult);                   // 3
    
        // 03.查看栈顶结果
        int peekResult = st.peek();
        System.out.println(peekResult);                  // 2
    
        // 04.判断是否为空
        boolean isEmpty = st.isEmpty();
        System.out.println(isEmpty);                     // false
    
        // 05.获取元素个数
        int size = st.size();
        System.out.println(size);                        // 2
    
        // 06.转成数组 - 类型必须是引用数据类型
        Integer[] stToArr = st.toArray(new Integer[0]);
        System.out.println(Arrays.toString(stToArr));    // [1, 2]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

    2.Queue - 单端队列

    定义

    Queue<Integer> queue = new LinkedList<>();
    
    • 1

    使用 LinkedList 实现即可,这也几乎是LinkedList最常见的用途了。

    API

    API功能概述备注
    boolean offer(T value)插入新的元素支持向上转型,因此value可以是基本数据类型
    若队列已满,返回false
    T poll( )队首元素出队若队列为空,返回null
    T peek( )查看队首元素(不移除)不会移除该元素
    若队列为空,返回null
    boolean isEmpty( )判断队列是否为空
    int size( )返回队列的元素个数
    T[] toArray(T[] arrObj)将队列转换成数组传入的参数是一个引用数据类型的数组对象
    举例:Integer[] arr = queue.toArray(new Integer[0])

    示例代码

    public static void testQueue(){
        Queue<Integer> queue = new LinkedList<>();
    
        // 01.入队
        queue.offer(1);
        queue.offer(2);
        queue.offer(3);
    
        // 02.出队
        int pollResult = queue.poll();
        System.out.println(pollResult);         // 1
    
        // 03.查看队首结果
        int peekResult = queue.peek();
        System.out.println(peekResult);         // 2
    
        // 04.判断队列是否为空
        boolean isEmpty = queue.isEmpty();      // false
        System.out.println(isEmpty);
    
        // 05.获取元素个数
        int size = queue.size();                // 2
        System.out.println(size);
    
        // 06.转成数组
        Integer[] queueToArr = queue.toArray(new Integer[0]);
        System.out.println(Arrays.toString(queueToArr));    // [2, 3]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    3.Deque - 双端队列

    定义

    Deque<Integer> dq = new LinkedList<>();
    
    • 1

    双端队列同样使用 LinkedList

    API

    双端队列中,offer(T value)、poll( )、peek( )方法依旧是可用的

    但既然是双端队列,建议还是按照其特性使用与之等价的 offerLast(T value)、pollFirst( ) 和 peekFirst( )。

    API功能概述备注
    boolean offerFirst(T value)队首插入新的元素支持向上转型,因此value可以是基本数据类型
    若队列已满,返回false
    boolean offerLast(T value)队尾插入新的元素,与offer(T value)等价支持向上转型,因此value可以是基本数据类型
    若队列已满,返回false
    T pollFirst( )队首元素出队,与poll( )等价若队列为空,返回null
    T pollLast( )队首元素出队若队列为空,返回null
    T peekFirst( )查看队首元素(不移除),与peek( )等价不会移除该元素
    若队列为空,返回null
    T peekLast( )查看队首元素(不移除)不会移除该元素
    若队列为空,返回null
    boolean isEmpty( )判断队列是否为空
    int size( )返回队列的元素个数
    T[] toArray(T[] arrObj)将队列转换成数组传入的参数是一个引用数据类型的数组对象
    举例:Integer[] arr = dq.toArray(new Integer[0])

    示例代码

    public static void testDeque() {
        Deque<Integer> dq = new LinkedList<>();
    
        // 01.队头入队
        dq.offerFirst(3);
        dq.offerFirst(2);
        dq.offerFirst(1);
    
        // 02.队尾入队 - offer(T value) 与 offerLast(T value)等价
        dq.offer(4);
        dq.offerLast(5);
    
        // 03.队头出队 - poll( ) 与 pollLast( )等价
        int pollResult = dq.poll();
        int pollFirstResult = dq.pollFirst();
        System.out.println(pollResult);                     // 1
        System.out.println(pollFirstResult);                // 2
    
        // 04.队尾出队
        int pollLastResult = dq.pollLast();
        System.out.println(pollLastResult);                 // 5
    
        // 05.查看队首结果 - peek( ) 与 peekFirst( )等价
        int peekResult = dq.peek();
        int peekFirstResult = dq.peekFirst();
        System.out.println(peekResult);                     // 3
        System.out.println(peekFirstResult);                // 3
    
        // 06.查看队尾结果
        int peekLastResult = dq.peekLast();                 // 4
        System.out.println(peekLastResult);
    
        // 07.判断队列是否为空
        boolean isEmpty = dq.isEmpty();                     // false
        System.out.println(isEmpty);
    
        // 08.获取元素个数
        int size = dq.size();                               // 2
        System.out.println(size);
    
        // 09.转成数组
        Integer[] queueToArr = dq.toArray(new Integer[0]);
        System.out.println(Arrays.toString(queueToArr));    // [3, 4]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
  • 相关阅读:
    冒泡排序和鸡尾酒排序和快速排序
    Day13-Linux系统用户管理知识精讲2
    火山引擎 DataLeap 的 Data Catalog 系统公有云实践
    2022谷粒商城学习笔记(二十一)购物车相关功能
    Java多线程使用 CountDownLatch等待其他线程执行完成
    Springboot 国际化
    第12章_数据库其它调优策略
    HazelEngine 学习记录 - Shader Asset Files
    【轨道机器人】成功驱动伺服电机(学生电源、DCH调试软件、DH系列伺服驱动器)
    计算机毕业设计Python+Django的大学生提问论坛系统
  • 原文地址:https://blog.csdn.net/xyxyxyxyxyxyx/article/details/126006458