* @Author Mr.Wu
* @Date 2019/6/7 21:40
* @Version 1.0
**/
public class shuzu01 {
public static void main(String[] args) {
int[][] chessArray = new int[11][11];
chessArray[1][2]=1;
chessArray[2][3]=2;
for(int[] rows: chessArray){
for (int data:rows){
System.out.printf("%d\t",data);
}
System.out.println();
}
int sum = 0;
for (int i = 0; i <11 ; i++) {
for (int j = 0; j <11 ; j++) {
if(chessArray[i][j]!=0){
sum++;
}
}
}
int[][] sparseArr = new int[sum+1][3];
sparseArr[0][0]=11;
sparseArr[0][1]=11;
sparseArr[0][2]=sum;
int count = 1;
for (int i = 0; i <11 ; i++) {
for (int j = 0; j <11 ; j++) {
if(chessArray[i][j]!=0){
sparseArr[count][0]=i;
sparseArr[count][1]=j;
sparseArr[count][2]=chessArray[i][j];
count++;
}
}
}
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("二维数组的长度");
System.out.println(sparseArr.length);
System.out.println("恢复之后的二维数组");
int[][] array = new int[sparseArr[0][0]][sparseArr[0][1]];
for (int i = 1; i <sparseArr.length ; i++) {
array[sparseArr[i][0]][sparseArr[i][1]]=sparseArr[i][2];
}
for(int[] rows:array){
for(int data:rows){
System.out.printf("%d\t",data);
}
System.out.println();
}
}
}