Java 固定长度 List 实现先进先出(FIFO)队列

在 Java 编程中,队列(Queue)是一种非常重要的数据结构,它遵循先进先出(FIFO: First-In-First-Out)的原则。本文将探讨如何使用 Java 中的固定长度 List 来实现一个先进先出的队列,并提供代码示例、甘特图和表格,以帮助你更好地理解这一概念。

1. 概述

FIFO 队列是一种数据结构,其中最早插入的元素也最早被移除。常见的实际应用包括任务调度、打印队列等。Java 提供了多种实现队列的方式,本文将重点关注使用 ArrayListLinkedList 来实现一个固定长度的 FIFO 队列。

2. 固定长度队列的实现

2.1 使用 ArrayList 实现 FIFO 队列

使用 ArrayList 可以相对简单地实现 FIFO 队列。我们需要保持一个固定长度的列表,同时管理元素的添加与删除。以下是实现的示例代码:

import java.util.ArrayList;

public class FixedSizeQueue<T> {
    private final int capacity;
    private final ArrayList<T> queue;
    private int head;
    private int size;

    public FixedSizeQueue(int capacity) {
        this.capacity = capacity;
        this.queue = new ArrayList<>(capacity);
        this.head = 0;
        this.size = 0;
    }

    public void enqueue(T element) {
        if (size < capacity) {
            queue.add(element);
            size++;
        } else {
            queue.set(head, element);
            head = (head + 1) % capacity;
        }
    }

    public T dequeue() {
        if (size == 0) {
            throw new IllegalStateException("Queue is empty");
        }
        T element = queue.get(head);
        head = (head + 1) % capacity;
        size--;
        return element;
    }

    public int getSize() {
        return size;
    }
}

2.2 使用 LinkedList 实现 FIFO 队列

虽然 ArrayList 是一种简单而高效的选择,LinkedList 也可以实现 FIFO 队列,尤其是在频繁添加和删除元素的情况下。以下是使用 LinkedList 的实现:

import java.util.LinkedList;

public class FixedSizeQueueLinkedList<T> {
    private final int capacity;
    private final LinkedList<T> queue;

    public FixedSizeQueueLinkedList(int capacity) {
        this.capacity = capacity;
        this.queue = new LinkedList<>();
    }

    public void enqueue(T element) {
        if (queue.size() == capacity) {
            queue.removeFirst(); // 移除头部元素
        }
        queue.addLast(element); // 添加新元素到尾部
    }

    public T dequeue() {
        if (queue.isEmpty()) {
            throw new IllegalStateException("Queue is empty");
        }
        return queue.removeFirst(); // 移除并返回头部元素
    }

    public int getSize() {
        return queue.size();
    }
}

3. 示例

我们可以通过以下示例代码来验证所实现的队列:

public class Main {
    public static void main(String[] args) {
        FixedSizeQueue<Integer> queue1 = new FixedSizeQueue<>(3);
        queue1.enqueue(1);
        queue1.enqueue(2);
        queue1.enqueue(3);
        System.out.println(queue1.dequeue()); // 输出: 1
        queue1.enqueue(4); // 1 将被替换
        System.out.println(queue1.dequeue()); // 输出: 2

        FixedSizeQueueLinkedList<Integer> queue2 = new FixedSizeQueueLinkedList<>(3);
        queue2.enqueue(1);
        queue2.enqueue(2);
        queue2.enqueue(3);
        System.out.println(queue2.dequeue()); // 输出: 1
        queue2.enqueue(4); // 1 将被替换
        System.out.println(queue2.dequeue()); // 输出: 2
    }
}

4. 甘特图

下图展示了 FIFO 队列的基本操作过程,我们可以把入队(Enqueue)和出队(Dequeue)操作视为任务,并用甘特图表示它们的执行顺序。

gantt
    title FIFO Queue Operations
    dateFormat  YYYY-MM-DD
    section Queue Operations
    Enqueue Element 1    :a1, 2023-10-01, 1d
    Enqueue Element 2    :a2, 2023-10-02, 1d
    Enqueue Element 3    :a3, 2023-10-03, 1d
    Dequeue Element 1    :a4, 2023-10-04, 1d
    Enqueue Element 4    :a5, 2023-10-05, 1d
    Dequeue Element 2    :a6, 2023-10-06, 1d

5. 小结

在本文中,我们探讨了如何在 Java 中使用固定长度的 List 实现先进先出的队列。我们通过 ArrayListLinkedList 两种方式实现了 FIFO 队列,并给出了具体的代码示例和简单的性能分析。在实际开发中,根据需求选择合适的队列实现方式至关重要。

固定长度队列在任务调度、资源管理等领域有着广泛的应用。在选择实现时,开发者需要考虑性能需求和数据类型,以确保系统高效运行。如果有其他相关问题,欢迎讨论和交流。