3.1 Sparse sparsearray array
3.1.1 First look at an actual demand
In the backgammon program written, there are functions of saving, exiting and reloading .
Analysis of the problem: Because many values of the two-dimensional array are the default value 0. Therefore, a lot of meaningless data
is recorded . -> Sparse array
3.1.2 Basic introduction
When most elements in an array are 0, or an array of the same value, you can use a sparse array to save the array.
Sparse arrays are handled by .
- How many rows and columns are there in the record array , and how many different values are there?
- Record the rows, columns and values of elements with different values in a small-scale array, thereby reducing the size of the program
Sparse array example
3.1.3 Application examples
- Use sparse arrays, to keep 2D arrays like the previous ones (chessboards, maps, etc.)
- Save the sparse array and restore the original two-dimensional array
- Overall thinking analysis
- the code
/**
* 稀疏数组
*/
public class SparseArray {
/**
* 二维数组 转 稀疏数组的思路
* 1. 遍历 原始的二维数组,得到有效数据的个数 sum
* 2. 根据sum 就可以创建 稀疏数组 sparseArr int[sum + 1] [3]
* 3. 将二维数组的有效数据数据存入到 稀疏数组
* <p>
* 稀疏数组转原始的二维数组的思路
* <p>
* 1. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,比如上面的 chessArr2 = int [11][11]
* 2. 在读取稀疏数组后几行的数据,并赋给 原始的二维数组 即可.
*/
/**
* 有效数据的个数
*/
static int sum = 0;
public static void main(String[] args) {
//创建一个原始的二维数组 11*11
int chessArrSource[][] = new int[11][11];
//稀疏数组
int sparseArr[][] = null;
//转换后的 二维数组
int chessArrConvert[][] = null;
//1.默认棋盘
defaultChessBoard(chessArrSource);
//2.二维数组 转 稀疏数组
sparseArr = toSparseArray(chessArrSource, sparseArr);
//3.稀疏数组 转 二维数组
toDoubleDimensionalArray(sparseArr, chessArrConvert);
}
/**
* 1.默认棋盘
*
* @param chessArrSource
*/
public static void defaultChessBoard(int chessArrSource[][]) {
//0:没有棋子,1:黑棋子,2:蓝棋子
chessArrSource[1][2] = 1;
chessArrSource[2][3] = 2;
//输出原始的二维数组
System.out.println("原始的二维数组:");
/**
* 第一维的数组长度是:intArray.length;
* 第二维的数组长度是:intArray[index].length;
* 注: index即索引(下标)的意思。
*/
for (int[] row : chessArrSource) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
}
/**
* 2.二维数组 转 稀疏数组
*/
public static int[][] toSparseArray(int chessArrSource[][], int sparseArr[][]) {
//2.二维数组 转 稀疏数组的思路
//2.1. 遍历 原始的二维数组,得到有效数据的个数 sum
for (int[] row : chessArrSource) {
for (int data : row) {
if (data != 0) {
sum++;
}
}
}
//2.2. 根据sum 就可以创建 稀疏数组 sparseArr int[sum + 1] [3]
sparseArr = new int[sum + 1][3];
sparseArr[0][0] = chessArrSource.length;//行数
sparseArr[0][1] = chessArrSource.length;//列数
sparseArr[0][2] = sum;//有效数据个数
//2.3. 将二维数组的有效数据数据存入到 稀疏数组
int count = 0;//count用于记录是第几个非0数据
for (int i = 1; i < chessArrSource.length; i++) {
for (int j = 1; j < chessArrSource.length; j++) {
if (chessArrSource[i][j] != 0) {
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = chessArrSource[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]);
}
return sparseArr;
}
/**
* 3.稀疏数组 转 二维数组
*/
public static void toDoubleDimensionalArray(int sparseArr[][], int chessArrConvert[][]) {
//稀疏数组转原始的二维数组的思路
//1. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,比如上面的 chessArr2 = int [11][11]
chessArrConvert = new int[sparseArr[0][0]][sparseArr[0][1]];
//2. 在读取稀疏数组后几行的数据,并赋给 原始的二维数组 即可.
for (int i = 1; i < sparseArr.length; i++) {
chessArrConvert[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
//输出转换后的二维数组
System.out.println();
System.out.println("转换后的二维数组:");
for (int[] row : chessArrConvert) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
}
}
原始的二维数组:
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
稀疏数组:
11 11 2
1 2 1
2 3 2
转换后的二维数组:
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
- Exercise requirements:
On the basis of the previous ones, save the sparse array to the disk, for example,
when map.data restores the original array, read map.data to restore