Preface
This article mainly explains sparse sparsearray arrays
Data structure and algorithm article list
Data structure and algorithm article list: click here to jump to view
table of Contents
(1) Demand
In the written Gomoku program, there are functions of saving, exiting and replaying
: Because many values of the two-dimensional array are the default value of 0, a lot of meaningless data is recorded, so a sparse array is used to solve this problem.
(2) Basic introduction
When most of the elements in an array are 0 or an array of the same value, you can use a sparse array to save the array.
The processing method for sparse arrays is:
(1) How many rows and columns are there in the record array, and how many different values are there (sparse arrays have a total of 3 columns)
(2) Record the rows, columns and values of elements with different values in a small The size of the array, thereby reducing the size of the program
(3) Application examples
(1) Use sparse arrays to retain the two-dimensional arrays similar to the previous ones (checkerboards, maps, etc.)
(2) Save the sparse arrays, and restore the original two-dimensional array numbers
(3) The following is the overall analysis
The idea of converting a two-dimensional array to a sparse array :
- Traverse the original two-dimensional array to get the number of valid data sum
- According to the sum can create a sparse array sparseArr int[sum + 1] [3]
- Store the valid data of the two-dimensional array into the sparse array
The idea of converting sparse array to original two-dimensional array :
- First read the first row of the sparse array, and create the original two-dimensional array based on the data in the first row, such as chessArr2 = int above [11][11]
- Just read the last few rows of the sparse array and assign it to the original two-dimensional array.
(4) Code implementation
package com.lzacking.sparsearray;
public class SparseArray {
public static void main(String[] args) {
// 创建一个原始的二维数组 11 * 11
// 0: 表示没有棋子, 1 表示黑子 2表示白子
int chessArr1[][] = new int[11][11];
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
chessArr1[4][5] = 2;
// 输出原始的二维数组
System.out.println("原始的二维数组~~");
for (int[] row : chessArr1) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
// 将二维数组转稀疏数组的思路
// 1. 先遍历二维数组 得到非0数据的个数
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr1[i][j] != 0) {
sum++;
}
}
}
// 2. 创建对应的稀疏数组
int sparseArr[][] = new int[sum + 1][3];
// 给稀疏数组赋值
sparseArr[0][0] = 11;
sparseArr[0][1] = 11;
sparseArr[0][2] = sum;
// 遍历二维数组,将非0的值存放到 sparseArr中
int count = 0; // count 用于记录是第几个非0数据
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr1[i][j] != 0) {
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = chessArr1[i][j];
}
}
}
// 输出稀疏数组的形式
System.out.println();
System.out.println("得到稀疏数组为~~~~");
for (int i = 0; i < sparseArr.length; i++) {
System.out.printf("%d\t%d\t%d\t\n", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
}
System.out.println();
//将稀疏数组 --》 恢复成 原始的二维数组
/*
* 1. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,比如上面的 chessArr2 = int [11][11]
2. 在读取稀疏数组后几行的数据,并赋给 原始的二维数组 即可.
*/
//1. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组
int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
//2. 在读取稀疏数组后几行的数据(从第二行开始),并赋给 原始的二维数组 即可
for(int i = 1; i < sparseArr.length; i++) {
chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
// 输出恢复后的二维数组
System.out.println();
System.out.println("恢复后的二维数组");
for (int[] row : chessArr2) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
}
}
Result :