java数组实现队列_使用数组在Java中进行队列实现

java数组实现队列

什么是队列? (What is a Queue?)

Queue is a special type of data structure, which is designed to hold elements before processing and process elements in a FIFO (first-in-first-out) manner. It’s part of java collections framework. In this tutorial, we will learn Queue implementation in Java using an array.

队列是一种特殊类型的数据结构,旨在在处理和处理元素之前以FIFO(先进先出)的方式保存元素。 它是java collections框架的一部分。 在本教程中,我们将学习如何使用数组在Java中实现Queue。

基本队列功能 (Basic Queue Functions)

A Queue must have the following functions:

队列必须具有以下功能:

  • enqueue(obj) – insert element to the queue.

    enqueue(obj) –将元素插入队列。
  • dequeue() – remove and return the least recent item from the queue.

    dequeue() –从队列中删除并返回最近的项目。
  • isEmpty() – returns true if the queue is empty, else false.

    isEmpty() –如果队列为空,则返回true,否则返回false。

Java中的队列实现 (Queue Implementation in Java)

We can implement basic Queue functions using an array.

我们可以使用数组实现基本的Queue函数。

Here is the complete code to implement a Queue in Java.

这是在Java中实现队列的完整代码。

package com.journaldev.java;

public class MyQueue {

	public static final int DEFAULT_SIZE = 5;

	private Object data[];

	private int index;

	public MyQueue() {
		data = new Object[DEFAULT_SIZE];
	}

	public MyQueue(int size) {
		data = new Object[size];
	}

	public boolean isEmpty() {
		return index == 0;
	}

	public void enqueue(Object obj) throws Exception {
		if (index == data.length - 1) {
			throw new Exception("Queue is full. Dequeue some objects");
		}
		this.data[index] = obj;
		this.index++;
	}

	public Object dequeue() throws Exception {
		if (isEmpty())
			throw new Exception("Queue is empty");
		Object obj = this.data[0];
		for (int i = 0; i < this.index - 1; i++) {
			data[i] = data[i + 1];
		}
		this.index--;
		return obj;

	}
}

Important Points

重要事项

  • There are two constructors – one to create a Queue with default size, the other one is used to specify the queue size.

    有两个构造函数–一个用于创建默认大小的Queue,另一个用于指定队列的大小。
  • We are using a private integer variable “index” to manage the queue elements.

    我们正在使用私有整数变量“ index”来管理队列元素。
  • We are using an Object array so that we can hold any type of object in the Queue. We can use generics here too but I am avoiding that to keep the program simple.

    我们正在使用对象数组,以便可以在队列中保存任何类型的对象。 我们也可以在此处使用泛型,但为了避免使程序保持简单,我避免这样做。
  • If the queue is full, the enqueue() will throw an exception with proper message.

    如果队列已满,则enqueue()会引发带有适当消息的异常。
  • If the queue is empty and we call the dequeue() function, an exception is raised.

    如果队列为空,并且我们调用dequeue()函数,则会引发异常。

Implementation Limitations
The Queue methods are not synchronized and it’s not thread-safe. There is a chance of data inconsistency if you use this implementation in a multi-threaded environment.

实施局限性
Queue方法未同步,并且不是线程安全的。 如果在多线程环境中使用此实现,则可能会导致数据不一致。

测试程序以检查队列实施 (Test Program to Check Queue Implementation)

Let’s test our queue implementation with some calls to enqueue() and dequeue() functions.

让我们通过对enqueue()和dequeue()函数的一些调用来测试我们的队列实现。

MyQueue queue = new MyQueue();
queue.enqueue("1");
System.out.println(queue.dequeue());

queue.enqueue("2");
queue.enqueue("3");
queue.enqueue("4");
System.out.println(queue.dequeue());

queue.enqueue("5");
queue.enqueue("6");
// queue.enqueue("7");
// queue.enqueue("8");

Output:

输出:

1
2

If you uncomment the last two lines, the queue will get full and an exception will be raised.

如果取消注释最后两行,则队列将已满,并且将引发异常。

Exception in thread "main" java.lang.Exception: Queue is full. Dequeue some objects
	at com.journaldev.java.MyQueue.enqueue(MyQueue.java:25)
	at com.journaldev.java.MyQueue.main(MyQueue.java:56)

结论 (Conclusion)

A Queue is one of the simplest data structures. We can implement it using an array or a list.

队列是最简单的数据结构之一。 我们可以使用数组或列表来实现它。

翻译自: https://www.journaldev.com/104/queue-implementation-java-array

java数组实现队列

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值