package com.suredata.MQTT;
public class Xisushuzu {
public static void main(String[] args) {
//常见二维数组
int [][] qipan=new int[11][11];
qipan[1][2]=1;
qipan[2][2]=1;
qipan[2][3]=2;
qipan[3][2]=2;
qipan[1][4]=1;
qipan[1][3]=2;
qipan[3][3]=1;
//遍历行
for(int i=0;i<qipan.length;i++){
//遍历列
for(int j=0;j<qipan[i].length;j++){
System.out.print(qipan[i][j]+"\t");
}
System.out.println();
}
int xishushuzuArry[][]=commonToXishushuzu(qipan);
//遍历细数数组
//遍历行
for(int i=0;i<xishushuzuArry.length;i++){
//遍历列
for(int j=0;j<xishushuzuArry[i].length;j++){
System.out.print(xishushuzuArry[i][j]+"\t");
}
System.out.println();
}
}
public static int[][] commonToXishushuzu(int [][]commonArray){
//有效值总数
int count=0;
//总行数
int row=commonArray.length;
//总列数
int column=commonArray[0].length;
/*int column=0;
for(int k=0;k<row;k++){
column=Math.max(column,qipan[k].length);
}*/
//遍历行
for(int i=0;i<commonArray.length;i++){
//遍历列
for(int j=0;j<commonArray[i].length;j++){
if(commonArray[i][j]!=0){
count++;
}
}
}
//创建稀疏数组
int xishushuzuArry[][]=new int[count+1][3];
//为稀疏数组赋值
xishushuzuArry[0][0]=row;
xishushuzuArry[0][1]=column;
xishushuzuArry[0][2]=count;
int prerow=1;
//遍历行
for(int i=0;i<commonArray.length;i++){
//遍历列
for(int j=0;j<commonArray[i].length;j++){
if(commonArray[i][j]!=0){
//xishushuzuArry[1][0]=1;
//xishushuzuArry[1][1]=2;
//xishushuzuArry[1][2]=qipan[1][2];
xishushuzuArry[prerow][0]=i;
xishushuzuArry[prerow][1]=j;
xishushuzuArry[prerow][2]=commonArray[i][j];
prerow++;
}
}
}
return xishushuzuArry;
}
}