将原始的二维数组转换成稀疏数组实现压缩算法
核心代码如下
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();
}
SparseArray sparseArray = new SparseArray();
int[][] sparse = sparseArray.bigToSparse(chessArr1);
File file = new File("map.data");
FileOutputStream os = null;
ObjectOutputStream oos = null;
try {
os = new FileOutputStream(file);
oos = null;
oos = new ObjectOutputStream(os);
oos.writeObject(sparse);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (oos != null)
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (os != null)
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 将原始二维数组转换成稀疏数组
* @param chessArr1
* @return 稀疏数组
* @Author Hleon
*/
public int [][] bigToSparse(int [][] chessArr1){
// 将二维数组 转 稀疏数组的思
// 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();
return sparseArr;
}
/**
* 将稀疏数组 --》 恢复成 原始的二维数组
* 1. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,比如上面的 chessArr2 = int [11][11]
* 2. 在读取稀疏数组后几行的数据,并赋给 原始的二维数组 即可.
* @param sparseArr
* @return 原始的二维数组
* @Author Hleon
*/
public int [][] spraseToBig(int [][] sparseArr) {
//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();
}
return chessArr2;
}
@Test
public void test(){
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream("D:\\WordSpace\\javaProject\\IDEAwordspace\\map.data");
ois = new ObjectInputStream(fis);
int[][] arr = (int[][]) ois.readObject();
for (int[] k: arr) {
for (int v: k) {
System.out.print("\t"+v);
}
System.out.println();
}
SparseArray sparseArray = new SparseArray();
sparseArray.spraseToBig(arr);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if ( ois != null){
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}