package com.数据结构;
public class 稀疏数组 {
public static void main(String[] args) {
int[][] a = new int[11][11];
a[1][3] = 1;
a[2][4] = 2;
a[3][3] = 1;
a[3][4] = 2;
int sum = 0;
for(int[] r :a) {
for(int data : r) {
System.out.print(data+"\t");
if(data!=0) {
sum++;
}
}
System.out.println();
}
System.out.println("----------------------------------------------------------------------------------");
int[][] 稀疏数组 = new int[sum+1][3];
稀疏数组[0][0] = a.length;
稀疏数组[0][1] = a[0].length;
稀疏数组[0][2] = sum;
int count = 0;
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[i].length;j++) {
if(a[i][j]!=0) {
稀疏数组[++count][0] = i;
稀疏数组[count][1] = j;
稀疏数组[count][2] = a[i][j];
}
}
}
int[][] data = new int[稀疏数组[0][0]][稀疏数组[0][1]];
for(int i=1;i<稀疏数组.length;i++) {
data[稀疏数组[i][0]][稀疏数组[i][1]] = 稀疏数组[i][2];
}
for(int[] t :data) {
for(int temp :t) {
System.out.print(temp+"\t");
}
System.out.println();
}
}
}