线性结构:
数组、链表、队列、栈
非线性结构:
二维数组、稀疏数组、广义表、树、图
稀疏数组
数组中大多数元素为0时考虑使用
原理:记录几行几列,有多少个不为0的值
应用:保存棋盘、地图
压缩<->恢复
代码实现:
public class Demo {
private static final String DIR = "D://sparseArr.data";
public static void main(String[] args) {
// 定义原始数组
int[][] chessArrOrg = new int[11][11];
chessArrOrg[1][2] = 1;
chessArrOrg[2][3] = 2;
chessArrOrg[4][4] = 2;
System.out.println("定义的原始数组:");
for (int[] row : chessArrOrg) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
// 压缩为稀疏数组
int rowLen = chessArrOrg.length;
int colLen = chessArrOrg[0].length;
int sum = 0;
for (int[] row : chessArrOrg) {
for (int data : row) {
if (data != 0) {
sum++;
}
}
}
int[][] sparseArr = new int[sum+1][3];
sparseArr[0][0] = rowLen;
sparseArr[0][1] = colLen;
sparseArr[0][2] = sum;
int rowIndex = 0;
for (int i = 0; i < rowLen; i++) {
for (int j = 0; j < colLen; j++) {
if (chessArrOrg[i][j] != 0) {
rowIndex++;
sparseArr[rowIndex][0] = i;
sparseArr[rowIndex][1] = j;
sparseArr[rowIndex][2] = chessArrOrg[i][j];
}
}
}
System.out.println("压缩后的稀疏数组:");
for (int[] row : sparseArr) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
// 存盘
System.out.println("将压缩后的稀疏数组存入磁盘...");
try {
FileWriter fileWriter = new FileWriter(DIR);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
for (int[] row : sparseArr) {
StringBuilder sb = new StringBuilder();
for (int data : row) {
sb.append(data).append(" ");
}
bufferedWriter.write(sb.toString());
bufferedWriter.newLine();
}
bufferedWriter.close();
} catch (Exception e) {
e.printStackTrace();
}
// 从磁盘中读取
System.out.println("从磁盘中读取...");
List<Integer> list = new ArrayList<>();
try {
FileReader fileReader = new FileReader(DIR);
BufferedReader bufferedReader = new BufferedReader(fileReader);
String content = null;
while ((content = bufferedReader.readLine()) != null) {
String[] strNums = content.split(" ");
for (int i = 0; i < strNums.length; i++) {
list.add(Integer.parseInt(strNums[i]));
}
}
} catch (Exception e) {
e.printStackTrace();
}
// 恢复为稀疏数组
System.out.println("恢复为稀疏数组...");
int sparseArrRow = list.get(2) + 1;
int[][] sparseArr2 = new int[sparseArrRow][3];
rowIndex = 0;
for (int i = 0; i < list.size(); i=i+3) {
sparseArr2[rowIndex][0] = list.get(i);
sparseArr2[rowIndex][1] = list.get(i+1);
sparseArr2[rowIndex][2] = list.get(i+2);
rowIndex++;
}
System.out.println("恢复后的稀疏数组...");
for (int[] row : sparseArr2) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
// 恢复为原始数组
System.out.println("稀疏数组还原为原始数组...");
int[][] chessArr2 = new int[ sparseArr2[0][0] ][ sparseArr2[0][1] ];
for (int i = 1; i < sparseArr2.length; i++) {
chessArr2[ sparseArr2[i][0] ][sparseArr2[i][1]] = sparseArr2[i][2];
}
System.out.println("恢复后的原始数组...");
for (int[] row : chessArr2) {
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 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
压缩后的稀疏数组:
11 11 3
1 2 1
2 3 2
4 4 2
将压缩后的稀疏数组存入磁盘...
从磁盘中读取...
恢复为稀疏数组...
恢复后的稀疏数组...
11 11 3
1 2 1
2 3 2
4 4 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 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
Process finished with exit code 0