Sparse array (sparsearray)
A look at the actual demand
331 procedures written, there are functions to save and exit and continued on disk.
analyse problem:
Because a lot of the value of the two-dimensional array is the default value 0, and therefore makes no sense to record a lot of data .-> sparse array.
basic introduction
- When most of the elements in an array is 0, or a value of the same array, the sparse array can be used to hold the array.
Sparse array of treatment methods are:
- An array of records, a total of several odd row, how many different values
- And the ranks of the value of records with different values of the elements in an array of small-scale, thereby reducing the size of the program
Illustrates the sparse array
The first line sparse array value: display original two-dimensional array has several odd row several valid value!
Then one by one began to record where the rms value of the ranks!
Conversion of two-dimensional arrays and sparse arrays
Example: FIG.
Two-dimensional array to a sparse array of ideas:
- Traversing the original two-dimensional array, the number of valid data obtained sum
- The sum can create a sparse array sparseArr int [sum + 1] [3]
- The two-dimensional array of valid data stored in the sparse array
Sparse array to a two-dimensional array of ideas:
- First reads the first row of the sparse array, according to the data of the first row, to create the original two-dimensional array, such as the above chessArr2 = int [11] [11]
- And then read a few lines of the sparse array, and assigned to the original two-dimensional array can be!
Code:
package com.atguigu.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++;
}
}
}
// 遍历二维数组,将非 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];
}
}
}
// 2. 创建对应的稀疏数组
int sparseArr[][] = new int[sum + 1][3]; // 给稀疏数组赋值
sparseArr[0][0] = 11;
sparseArr[0][1] = 11;
sparseArr[0][2] = sum;
// 输出稀疏数组的形式
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();
}
}
}