一、稀疏数组的实现
一般的二维数组中,如果存在太多重复值0,就是说整个数组存放了很多无意义的数组,这会消耗不必要的内存空间。
1.二维数组转稀疏数组的思路**
(1)遍历原始的二维数组,得到有意义数据的个数sum
(2).根据sum就可以创建稀疏数组sparseArr[sum+1][3]
(3).将二维数组的有意义数据存入稀疏数组中
2.稀疏数组转原始数组的思路**
(1).先读取稀疏数组的第一行,从第一行获取原始数组的行列数,比如上面数组就是11*11
(2).再读取稀疏数组后几行的数据,并赋给原始二维数组即可
代码实现:
public class SparseArray {
public static void main(String[] args) {
//定义11*11原始数组
int[][] originalArray = new int[11][11];
originalArray[1][2] = 1;
originalArray[2][3] = 2;
System.out.println("原始二维数组:");
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
System.out.print(originalArray[i][j]+"\t");
}
System.out.println();
}
System.out.println();
//1.先遍历二位数组,得到非零数据个数
int sum = 0;
for (int i = 0; i < originalArray.length; i++) {
for (int j = 0; j < originalArray.length; j++) {
if(originalArray[i][j] != 0){
sum++;
}
}
}
//2.创建对应的稀疏数组
int[][] sparseArray = new int[sum+1][3];
sparseArray[0][0] = 11;
sparseArray[0][1] = 11;
sparseArray[0][2] = sum;
//3.再次遍历原始数组,给稀疏数组赋值
int count = 0;
for (int i = 0; i < originalArray.length; i++) {
for (int j = 0; j < originalArray.length; j++) {
if(originalArray[i][j] != 0){
count++;
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = originalArray[i][j];
}
}
}
//4.输出稀疏数组
System.out.println("组建后稀疏数组:");
for (int i = 0; i < sum+1; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(sparseArray[i][j] + "\t");
}
System.out.println();
}
}
}
二、数组模拟循环队列
1.使用数组模拟环形队列思路分析
(1).front变量的含义做一个调整:front就指向队列的第一个元素,即arr[front]就是队列的第一个元素,front的初始值为0
(2).rear变量的含义做一个调整:rear指向队列的最后一个元素的后一个位置,因为希望空出一个位置作为约定,rear的初始值为0
(3).当队列满时,条件为(rear+1)%maxSize=front —满
(4).当队列为空时,条件为rear=front —空
(5).当我们这样分析时,队列中有效的个数为(rear+maxSize-front)%maxSize
(6).在之前的数组队列上进行修改,得到一个环形队列
代码实现:
public class ArrayQueueCircle {
public static void main(String[] args) {
CircleArray circleArray = new CircleArray(4);
Scanner scanner = new Scanner(System.in);
char key = ' ';
boolean isFlag = true;
while (isFlag){
System.out.println("s(show):展示队列");
System.out.println("a(add):添加数据到队列");
System.out.println("g(get):取出队列数据");
System.out.println("h(head):展示队列首元素");
System.out.println("e(exit):退出程序");
key = scanner.next().charAt(0);
switch (key){
case 's':
circleArray.show();
break;
case 'a':
System.out.println("请输入要添加的数据:");
int number = scanner.nextInt();
circleArray.add(number);
break;
case 'g':
try {
int getNumber = circleArray.get();
System.out.println("取出数据为:" + getNumber);
} catch (Exception e) {
e.printStackTrace();
}
break;
case 'h':
try {
int first = circleArray.head();
System.out.println("首元素为:" + first);
} catch (Exception e) {
e.getMessage();
}
break;
case 'e':
scanner.close();
isFlag = false;
System.out.println("退出程序");
break;
}
}
}
}
class CircleArray{
private int maxsize;
private int front;
private int rear;
private int[] arr;
public CircleArray(int maxsize) {
this.maxsize = maxsize;
front = 0;
rear = 0;//rear此时指向最后一个数据的后一个位置
arr = new int[maxsize];
}
public boolean isFull(){
return (rear + 1) % maxsize == front;
}
public boolean isEmpty(){
return rear == front;
}
public void add(int n){
if(isFull()){
System.out.println("队列已满,不能添加数据");
}else{
arr[rear] = n;
rear = (rear + 1) % maxsize;
}
}
public void show(){
if(isEmpty()){
System.out.println("队列为空,无数据");
} else {
int res = size();
for (int i = front; i < front + res; i++) {
System.out.println("arr[" + i + "] = " + arr[i]);
}
}
}
public int get(){
if(isEmpty()){
throw new RuntimeException("队列为空,没有数据");
}else {
int tem = arr[front];
front = (front + 1) % maxsize;
return tem;
}
}
public int head(){
if(isEmpty()){
throw new RuntimeException("队列为空,没有数据");
}else {
return arr[front];
}
}
//求出当前队列的有效数据个数
public int size(){
return (rear + maxsize -front) % maxsize;
}
}