Java 1.8使用數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列
本文實(shí)例為大家分享了Java 1.8使用數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列的具體代碼,供大家參考,具體內(nèi)容如下
1、引入
使用數(shù)組實(shí)現(xiàn)循環(huán)隊(duì)列,功能如下:
1)isFull():隊(duì)列滿?
2)isEmpty():隊(duì)列空?
3)add():添加元素。
4)pop():移除元素。
5)display():展示隊(duì)列。
6)getSize():獲取當(dāng)前隊(duì)列元素個(gè)數(shù)。
2、代碼
package DataStructure;
import java.util.Arrays;
/**
* @author: Inki
* @email: inki.yinji@qq.com
* @create: 2020 1022
* @last_modify: 2020 1023
*/
public class MyArrayQueue<AnyType> {
/**
* The default max size of my array queue.
*/
private final int DEFAULT_MAX_SIZE = 10;
/**
* The max size of my array queue.
*/
private int maxSize;
/**
* The front of my array queue.
*/
private int front;
/**
* The rear of my array queue.
*/
private int rear;
/**
* Using array to simulate queue.
*/
private AnyType[] arrQueue;
/**
* The first constructor.
*/
public MyArrayQueue() {
this(DEFAULT_MAX_SIZE);
}//Of the first constructor
/**
* The second constructor.
*/
public MyArrayQueue(int paraMaxSize) {
maxSize = paraMaxSize + 1;
arrQueue = (AnyType[]) new Object[maxSize];
front = 0;
rear = 0;
}//Of the second constructor
/**
* Queue is full?
* @return:
* True if full else false.
*/
public boolean isFull() {
return (rear + 1) % maxSize == front;
}//Of isFull
/**
* Queue is empty?
* @return:
* True if empty else false.
*/
public boolean isEmpty() {
return front == rear;
}//Of isEmpty
/**
* Add element.
* @param:
* paraVal:
* The given value.
*/
public void add(AnyType paraVal) {
if(isFull()) {
System.out.println("The queue is full.");
return;
}//Of if
arrQueue[rear] = paraVal;
rear = (rear + 1) % maxSize;
}//Of add
/**
* Pop element.
*/
public AnyType pop() {
if (isEmpty()) {
throw new RuntimeException("The queue is full.");
}//Of if
AnyType retVal = arrQueue[front];
front = (front + 1) % maxSize;
return retVal;
}//of pop
/**
* Display array queue.
*/
public void display() {
if (isEmpty()) {
System.out.println("The queue is empty.");
return;
}//Of if
System.out.print("The queue is: [");
int i = front;
while (i != (rear + maxSize- 1) % maxSize) {
System.out.printf("%s, ", arrQueue[i]);
i = (i + 1) % maxSize;
}//Of while
System.out.printf("%s]", arrQueue[rear - 1]);
}//Of display
/**
* Get current size of my array queue.
*/
public int getSize() {
return (rear - front + maxSize) % maxSize + 1;
}//Of getSize
/**
* The main
**/
public static void main(String[] args) {
MyArrayQueue <Integer> testArrayQueue = new MyArrayQueue<>(3);
testArrayQueue.add(1);
testArrayQueue.add(2);
testArrayQueue.add(4);
testArrayQueue.pop();
testArrayQueue.display();
}//Of main
}//Of MyArrayQueue
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
spring-boot中的SPI機(jī)制實(shí)例講解
這篇文章主要介紹了spring-boot中的SPI機(jī)制實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
Java無(wú)界阻塞隊(duì)列DelayQueue詳細(xì)解析
這篇文章主要介紹了Java無(wú)界阻塞隊(duì)列DelayQueue詳細(xì)解析,DelayQueue是一個(gè)支持時(shí)延獲取元素的無(wú)界阻塞隊(duì)列,隊(duì)列使用PriorityQueue來(lái)實(shí)現(xiàn),隊(duì)列中的元素必須實(shí)現(xiàn)Delayed接口,在創(chuàng)建元素時(shí)可以指定多久才能從隊(duì)列中獲取當(dāng)前元素,需要的朋友可以參考下2023-12-12
springboot整合redis修改分區(qū)的操作流程
這篇文章主要介紹了springboot整合redis修改分區(qū)的操作流程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07
使用Java注解和反射實(shí)現(xiàn)JSON字段自動(dòng)重命名
這篇文章主要介紹了如何使用Java注解和反射實(shí)現(xiàn)JSON字段自動(dòng)重命名,文中通過(guò)代碼示例和圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-08-08
教你在一分鐘之內(nèi)理解Java Lambda表達(dá)式并學(xué)會(huì)使用
今天給大家?guī)У奈恼率荍ava8新特性的相關(guān)知識(shí),文章圍繞著如何在一分鐘之內(nèi)理解Java Lambda表達(dá)式并學(xué)會(huì)使用展開,文中有非常詳細(xì)的介紹,需要的朋友可以參考下2021-06-06
Java漢字轉(zhuǎn)拼音工具類完整代碼實(shí)例
這篇文章主要介紹了java漢字轉(zhuǎn)拼音工具類完整代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03

