package shuzu;
import java.util.Arrays;
public class Demo08 {
public static void main(String[] args) {
//创建一个二维数组。0为黑,1为白
int[][] qipan = new int[11][11];
qipan[1][2] = 1;
qipan[2][3] = 2;
dayin(qipan);
//转化为稀疏字符
int sum =0;//读取非0,数字的个数
for (int i = 0; i < qipan.length; i++) {
for (int i1 = 0; i1 < qipan[i].length; i1++) {
if (qipan[i][i1]!=0){
sum++;
}
}
}
System.out.println("有效值个数: "+sum);
System.out.println("=======================================================");
//定义一个稀疏数组
int xishu[][]=new int[sum+1][3];
xishu[0][0]=11;
xishu[0][1]=11;
xishu[0][sum]=sum;
int k=1;
for (int i = 0; i < qipan.length; i++) {
for (int i1 = 0; i1 < qipan[i].length; i1++) {
if (qipan[i][i1]!=0){
xishu[k][0]=i;
xishu[k][1]=i1;
xishu[k][2]=qipan[i][i1];
k=k+1;
}
}
}
dayin(xishu);//打印稀疏数组的值
System.out.println();//换行
//读取稀疏数组,进行还原
System.out.println("还原");
int[][] huanyuan=new int[xishu[0][0]][xishu[0][1]];
for (int i = 1; i < xishu.length; i++) {
huanyuan[xishu[i][0]][xishu[i][1]]=xishu[i][2];
}
dayin(huanyuan);//打印还原数组
}
public static void dayin(int[][]erweishuzu){
//打印二维数组的方法
for (int[] intss : erweishuzu) {
for (int anInts : intss) {
System.out.print(anInts+"\t");
}
System.out.println();
}
}
}
标签:i1,int,sum,第六天,xishu,qipan,out
From: https://www.cnblogs.com/inian/p/17995332