二维数组压缩再还原
package cn.hlooc;
public class ArrayReduceTest {
public static void main(String[] args) {
int[][] arr = new int[11][11];
arr[1][2] = 1;
arr[2][3] = 2;
arr[2][8] = 1;
int sum = 0;
for (int[] int1 : arr) {
for (int int2 : int1) {
System.out.print(int2 + "\t");
if (int2 != 0) {
sum++;
}
}
System.out.println();
}
System.out.println("=========================");
int[][] arr2 = new int[sum + 1][3];
arr2[0][0] = 11;
arr2[0][1] = 11;
arr2[0][2] = sum;
int count = 0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] != 0) {
count++;
arr2[count][0] = i;
arr2[count][1] = j;
arr2[count][2] = arr[i][j];
}
}
}
for (int[] int1 : arr2) {
for (int int2 : int1) {
System.out.print(int2 + "\t");
}
System.out.println();
}
int[][] arr3 = new int[arr2[0][0]][arr2[0][1]];
for (int i = 1; i < arr2.length; i++) {
arr3[arr2[i][0]][arr2[i][1]] = arr2[i][2];
}
System.out.println("==================");
for (int[] int1 : arr3) {
for (int int2 : int1) {
System.out.print(int2 + "\t");
}
System.out.println();
}
}
}