美文网首页
JAVA稀疏数组,针对数组的压缩,简洁

JAVA稀疏数组,针对数组的压缩,简洁

作者: 司徒新新 | 来源:发表于2020-04-28 12:10 被阅读0次
WeChat2e541793de49fa5ec0f49263a1f7957d.png
        //1.创建一个二维数组 11* 11   0:没有棋子,   1黑棋   2白棋

        int[][] array1 = new int[11][11];
        array1[1][2] = 1;
        array1[2][3] = 1;

        System.out.println("输出原始数组");
        for (int[] ints:array1) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();

        }
        System.out.println("__________________________________________");

        //转换为稀疏数组保存
        //1.获取有效值的个数
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (array1[i][j] != 0) {
                    sum ++;
                }
            }
        }

        //2.创建一个稀疏数组的数组
        int[][] array2 = new int[sum + 1][3];
        array2[0][0] = 11;              //记录行
        array2[0][1] = 11;              //记录列
        array2[0][2] = sum;             //记录不为0的值有几个

        //3.遍历二维数组,将非0的值,存放稀疏稀疏数组中
        int count = 0;//记录是第几个不为0的值
        for (int i = 0; i < array1.length ; i++) {
            for (int j = 0; j < array1[i].length; j++) {
                if (array1[i][j] != 0) {
                    count++;
                    array2[count][0] = i;
                    array2[count][1] = j;
                    array2[count][2] = array1[i][j];
                }
            }
        }

        //输出稀疏数组
        for (int i = 0; i < array2.length ; i++) {
            System.out.println(array2[i][0] + "\t" + array2[i][1] + "\t" + array2[i][2] + "\t");
        }


        System.out.println("__________________________________________");
        System.out.println("还原");
        //1.读取稀疏数组
        int[][] array3 = new int[array2[0][0]][array2[0][1]];

        //2.给其中的元素还原它的值   i=0的时候 记录的是 总共有多少航多少列  几个值  所以从i = 1 开始
        for (int i = 1; i < array2.length; i++) {
            array3[array2[i][0]][array2[i][1]] = array2[i][2];
        }

        //3.打印
        System.out.println("打印还原的数组");
        for (int[] ints: array3) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();

        }

输出结果

输出原始数组
0   0   0   0   0   0   0   0   0   0   0   
0   0   1   0   0   0   0   0   0   0   0   
0   0   0   1   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
__________________________________________
11  11  2   
1   2   1   
2   3   1   
__________________________________________
还原
打印还原的数组
0   0   0   0   0   0   0   0   0   0   0   
0   0   1   0   0   0   0   0   0   0   0   
0   0   0   1   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   
0   0   0   0   0   0   0   0   0   0   0   

相关文章

网友评论

      本文标题:JAVA稀疏数组,针对数组的压缩,简洁

      本文链接:https://www.haomeiwen.com/subject/hyrvwhtx.html