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)文章
Java8新特性Lambda表達(dá)式的一些復(fù)雜用法總結(jié)
lambda表達(dá)式是JAVA8中提供的一種新的特性,它支持Java也能進(jìn)行簡(jiǎn)單的“函數(shù)式編程”。 下面這篇文章主要給大家介紹了關(guān)于Java8新特性Lambda表達(dá)式的一些復(fù)雜用法的相關(guān)資料,需要的朋友可以參考借鑒,下面來(lái)一起看看吧。2017-07-07
springboot配置多數(shù)據(jù)源的實(shí)例(MongoDB主從)
下面小編就為大家分享一篇springboot配置多數(shù)據(jù)源的實(shí)例(MongoDB主從),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
Java實(shí)現(xiàn)獲取Excel中的表單控件
Excel中可通過(guò)【開(kāi)發(fā)工具】菜單欄下插入表單控件,如文本框、單選按鈕、復(fù)選框、組合框等等。本文將利用Java實(shí)現(xiàn)獲取Excel中的表單控件,需要的可以參考一下2022-05-05
Java如何將int型數(shù)組轉(zhuǎn)為String型數(shù)組
這篇文章主要介紹了Java如何將int型數(shù)組轉(zhuǎn)為String型數(shù)組,本文給大家分享具體實(shí)現(xiàn)思路結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-03-03
HashSet如何保證元素不重復(fù)(面試必問(wèn))
HashSet 不保證集合的迭代順序,但允許插入 null 值,也就是說(shuō)它可以將集合中的重復(fù)元素自動(dòng)過(guò)濾掉,保證存儲(chǔ)在 HashSet 中的元素都是唯一的,這篇文章主要介紹了HashSet如何保證元素不重復(fù)(面試必問(wèn)),需要的朋友可以參考下2021-12-12
SpringBoot?Knife4j框架&Knife4j的顯示內(nèi)容的配置方式
Knife4j框架是基于Swagger2開(kāi)發(fā)的在線API文檔生成工具,主要功能包括自動(dòng)生成API文檔、接口文檔展示、接口測(cè)試工具、接口權(quán)限控制和在線調(diào)試,該框架支持通過(guò)注解自動(dòng)生成詳細(xì)的接口文檔,開(kāi)發(fā)者可以直接在文檔界面進(jìn)行接口測(cè)試和調(diào)試2024-09-09

