package itheima_shuju01;
public class chessArry01_xishushuzu {
public static void main(String[] args) {
// TODO Auto-generated method stub
int chessArry[][] = new int[11][11];
chessArry[1][2] = 1;
chessArry[2][3] = 2;
int acount = 0;
//棋盘输出
System.out.println("棋盘输出");
for(int []a1 :chessArry) {
for(int shucu : a1) {
System.out.print(shucu+" ");
if(shucu != 0 ) {
acount++;
}
}
System.out.println();
}
//棋盘--->稀疏数组
int shuArry[][] = new int[acount+1][3];
shuArry[0][0] = 11;
shuArry[0][1] = 11;
shuArry[0][2] = acount;
int a=0;
for(int i =0;i<11;i++) {
for(int j=0;j<11;j++) {
if(chessArry[i][j] != 0 ) {
a++;
shuArry[a][0] = i;
shuArry[a][1] = j;
shuArry[a][2] = chessArry[i][j];
}
}
}
//稀疏数组输出
System.out.println("稀疏数组输出");
for(int []a1 :shuArry) {
for(int shucu : a1) {
System.out.print(shucu+" ");
}
System.out.println();
}
//稀疏数组--->棋盘
System.out.println("稀疏数组--->棋盘");
int chessArry1[][] = new int[shuArry[0][0]][shuArry[0][1]];
for (int i1=1;i1<acount+1;i1++) {
chessArry1[shuArry[i1][0]][shuArry[i1][1]]=shuArry[i1][2];
}
System.out.println("棋盘输出");
for(int []a1 :chessArry1) {
for(int shucu : a1) {
System.out.print(shucu+" ");
}
System.out.println();
}
// for(int []a1 :chessArry) {
// for(int shucu : a1) {
// if(shucu != 0&&chessArry[a][2]==0) {
// chessArry1[a][2] = shucu;
//
// a++;
// }else {
// a++;
// }
// }
// }
}
}
代码运行图: