1.代码
1.QueueExample2
public class QueueExample2 {
public static void main(String[] args) {
MyArrayQueue myArrayQueue = new MyArrayQueue(3);
System.out.println("原始数组遍历:");
myArrayQueue.showQueue();
System.out.println("添加三个值后遍历");
myArrayQueue.addQueue(1);
myArrayQueue.addQueue(2);
myArrayQueue.addQueue(3);
myArrayQueue.showQueue();
System.out.println("取出的值:"+myArrayQueue.takeOutQueue());
System.out.println("取出的值:"+myArrayQueue.takeOutQueue());
myArrayQueue.showQueue();
}
private static class MyArrayQueue {
int maxSize;
int rear=-1;
int front=-1;
private int[] arr;
/**
*
* @param max 表示队列的最大长度
*/
public MyArrayQueue(int max) {
maxSize=max;
arr = new int[max];//设置长度
}
//遍历数组
public void showQueue() {
System.out.println("遍历数组");
for (int i : arr) {
System.out.println(i);
}
}
//添加数组数据
public void addQueue(int nextInt) {
if (isFull()) {//判断队列是否已经满了
System.out.println("数组最大长度为3,不会再添加");
}else {
arr[rear+1]=nextInt;
rear++;
}
System.out.println("数组的长度:"+rear);
}
//取出数据
public int takeOutQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空,不能取数据");
}
int i = arr[front+1];
arr[front+1]=0;
front++;
return i;
}
/**
* 队列是否已经满了
* @return true表示已经满了,false表示队列还没有满
*/
public boolean isFull(){
return rear==maxSize-1;
}
/**
* 队列是否为空
* @return true表示队列为空,false表示队列不为空
*/
public boolean isEmpty(){
return rear==front;
}
}
}
运行结果:
2.讲解
1.队列
1.介绍
是个先进先出的有序列表,可以用数组、链表来实现。(感觉就是为了解决排队这种问题而设置出来的东西,服务于需求)
2.应用
应用核酸检测排队,打新冠疫苗排队,电影排队,厕所排队,银行排队……
2.图解
1.图
下图是一个三步骤过程:创建空队列–>添加4个数据后的队列–>删除2个后的队列
1.解析图
只加不减。rear表示队列的后部,front表示队列的头部
3.代码讲解
其实就是用数组实现过程:创建队列(展示),添加数据(展示),取出数据(展示)
MyArrayQueue myArrayQueue = new MyArrayQueue(3);
MyArrayQueue myArrayQueue = new MyArrayQueue(3);
System.out.println("原始数组遍历:");
myArrayQueue.showQueue();
System.out.println("添加三个值后遍历");
myArrayQueue.addQueue(1);
myArrayQueue.addQueue(2);
myArrayQueue.addQueue(3);
myArrayQueue.showQueue();
System.out.println("取出的值:"+myArrayQueue.takeOutQueue());
System.out.println("取出的值:"+myArrayQueue.takeOutQueue());
myArrayQueue.showQueue();
1.创建队列,然后展示
创建队列对象
MyArrayQueue myArrayQueue = new MyArrayQueue(3);
构造方法
```java
int maxSize;
int rear=-1;
int front=-1;
private int[] arr;
/**
*
* @param max 表示队列的最大长度
*/
public MyArrayQueue(int max) {
maxSize=max;
arr = new int[max];//设置长度
}
展示
myArrayQueue.showQueue();
//遍历数组
public void showQueue() {
System.out.println("遍历数组");
for (int i : arr) {
System.out.println(i);
}
}
2.添加数据,然后展示
这里要进行判断队列是否已经满了
System.out.println("添加三个值后遍历");
myArrayQueue.addQueue(1);
myArrayQueue.addQueue(2);
myArrayQueue.addQueue(3);
myArrayQueue.showQueue();
//添加数组数据
public void addQueue(int nextInt) {
if (isFull()) {//判断队列是否已经满了
System.out.println("数组最大长度为3,不会再添加");
}else {
arr[rear+1]=nextInt;
rear++;
}
System.out.println("数组的长度:"+rear);
}
//队列是否已经满了
/**
* 队列是否已经满了
* @return true表示已经满了,false表示队列还没有满
*/
public boolean isFull(){
return rear==maxSize-1;
}
3.取出队列,然后展示,这里要判断是否为空,空的话,是无法获取的
System.out.println("取出的值:"+myArrayQueue.takeOutQueue());
System.out.println("取出的值:"+myArrayQueue.takeOutQueue());
myArrayQueue.showQueue();
//根据先进先出(取),来获取值
//取出数据
public int takeOutQueue() {
if (isEmpty()) {
throw new RuntimeException("队列为空,不能取数据");
}
int i = arr[front+1];
arr[front+1]=0;
front++;
return i;
}
判断是否为空,因为队列里的尾部rear,头部front都是只能是加的,那么它们相等的情况为值相等
/**
* 队列是否为空
* @return true表示队列为空,false表示队列不为空
*/
public boolean isEmpty(){
return rear==front;
}
3.总结反思
1.效率
其实排队这种增删比较频繁的使用链表会比较好,它增删快。而顺序链表的话是查询快,增删慢