Appearance
稀疏数组
基本介绍
当数组大部分元素为0或者同一个值时,用稀疏数组来保存。
- 处理方法:
- 记录行数、列数,有多少个不同的值。
- 把不同值的元素的行、列、值记录在一个小规模的数组中,缩小程序的规模
代码实现(Java)
package com.merlin.spaesearray;
public class SparseArray {
public static void main(String[] args) {
//创建一个原始的二维数组11*11
//0:表示没有棋子,1表示黑子,2表示蓝子
int[][] chessArray1 = new int[11][11];
chessArray1[1][2] = 1;
chessArray1[2][3] = 2;
chessArray1[4][5] = 2;
System.out.println("原始的二维数组:");
for (int[] row : chessArray1) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
//将二维数组 转 稀疏数组
//1、先遍历二维数组 得到非零数据的个数
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArray1[i][j] != 0) {
sum++;
}
}
}
//2、创建稀疏数组
int[][] sparseArray = new int[sum + 1][3];
//给稀疏数组赋值
sparseArray[0][0] = 11;
sparseArray[0][1] = 11;
sparseArray[0][2] = sum;
//遍历二维数组,将非0值存进稀疏数组
int count = 0;//count用于记录第几个非0数据
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArray1[i][j] != 0) {
count++;
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = chessArray1[i][j];
}
}
}
//输出稀疏数组
System.out.println();
System.out.println("得到稀疏数组为:");
for (int i = 0; i < sparseArray.length; i++) {
System.out.printf("%d\t%d\t%d\n", sparseArray[i][0], sparseArray[i][1], sparseArray[i][2]);
}
System.out.println();
//将稀疏数组恢复称稀疏数组
//1、先读取第一行,根据第一行的数据,创建原始的二维数组
//2、在读取稀疏数组后的几行数据,并赋值给原始的二维数组即可
int[][] chessArray2 = new int[sparseArray[0][0]][sparseArray[0][1]];
for (int i = 1; i < sparseArray.length; i++) {
chessArray2[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
}
//输出恢复后的稀疏数组
System.out.println("恢复的原始数组:");
for (int[] row : chessArray2) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
}
}