稀疏数组:如果有效数据大于无效数据,则需要稀疏数组进行压缩。
存储方式:
1)普通存储: 第一行存储原始数据总行数(row),总列数(),总的有效数据个数,
接下来每一行都存储有效数据的所在行,列和数据本身。
public class arrayqueue {
//使用数组模拟队列
private int maxSize;//数组最大容量
private int front;//队头
private int rear;//队尾
private int[] arr;//存放元素
//创建队列的构造器
public arrayqueue(int arrMaxSize) {
maxSize = arrMaxSize;//给定数组的最大值
int[] arr = new int[maxSize];//建立数组
front = -1;//开始时候指向队列头部的前一个为位置
rear = -1;//指向队列尾部的具体数据
}
//写一个方法,判断队列是否为满
public boolean isFull() {
return rear == maxSize - 1;
}
//判断队列是否为空
public boolean isEmpty() {
return rear == front;
}
//添加数据到队列
public void addQueue(int n) {
//判断队列是否满,如果满,就返回
if (isFull()) {
System.out.println("队列已满");
return;
}
//如果没满,指针rear后移,数据添加
rear++;
arr[rear] = n;
}
//获取队列的数据,加数据是从,rear开始,出队列是从front开始
public int getQueue() {
//判断队列是否为空
if (isEmpty()) {
//通过抛出异常处理
throw new RuntimeException("对列为空");
}
front++;
return arr[front];
}
//显示队列的所有数据
public void showQueue(){
//遍历数组
//判空
if (isEmpty()){
System.out.println("队列为空");
return;
}
for (int i = 0; i < arr.length; i++) {
System.out.println(i+","+arr[i]);
}
}
//显示队列的头数据是多少(注意,不是去出局)
public int headQueue(){
//判空操作
if(isEmpty()){
System.out.println("对列为空");
throw new RuntimeException("对列为空");
}
return arr[front+1];
}
}