跟随学习第二十四天
//用稀疏数组表示原始数组
public class ArraysDemo05 {
public static void main(String[] args) {
int[][] arrays = new int[8][9];
arrays[0][5] = 2;
arrays[0][6] = 3;
arrays[4][5] = 2;
arrays[4][6] = 3;
for (int[] ints : arrays) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
System.out.println("-----------------------------------");
//转换成稀疏数组
int sum = 0;
//遍历里面的有效值的个数
for (int i = 0; i < 8; i++) {
for (int j = 0; j < 9; j++) {
if(arrays[i][j]!=0){
sum++;
}
}
}
System.out.println("有效值的个数:" + sum);
//首先创建一个稀疏数组
int[][] arrays1 = new int[sum+1][3];
//这是数组的第一组数据,数组中有几行几列,还有刚刚遍历出来的有效值;
arrays1[0][0] = 8;
arrays1[0][1] = 9;
arrays1[0][2] = sum;
//遍历二维数组,将有效值存放到稀疏数组中
int count = 0;
for (int i = 0; i < arrays.length; i++) {
for (int j = 0; j < arrays[i].length; j++) {
if(arrays[i][j]!=0){
count++;
arrays1[count][0] = i;//i是原始数组的行数,将它赋值给稀疏数组中的行值中
arrays1[count][1] = j;//j是原始数组的列数,将它赋值给稀疏数组中的列值中
arrays1[count][2] =