15java稀疏数组

稀疏数组

稀疏数组简介:

如何表示以下稀疏数组?

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 2 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

稀疏数组的表示方法:

下面是自写的稀疏矩阵产生和压缩程序:

package Demoxx;

public class Demo02 {
    public static void main(String[] args) {
        int[][] array = new int[11][11];
        for (int i = 0; i < array.length; i++) {//产生这个稀疏矩阵
            for (int j = 0; j < array[i].length; j++) {
                if ((((i != 1) && (j != 2))) && (((i != 2) && (j != 3)))) {
                    array[i][j] = 0;
                } else if ((i == 1) && (j == 2)) {
                    array[i][j] = 1;
                } else if ((i == 2) && (j == 3)) {
                    array[i][j] = 2;
                }

            }
        }
        //大意了,int型数组的默认数值就是全0,可以直接给特定位置赋值也能产生上述稀疏数组
        //当然,如果稀疏数组里面的大量元素不是0而是其他数值,那么我的方法就有用了
        System.out.println("Sparse array:");
        print2Array(array);
        //人工压缩
        /*array 行  列  数字
           [0]  11  11  2
           [1]  1   2   1
           [2]  2   3   2
         */
        int[][] compression = {{11, 11, 2}, {1, 2, 1}, {2, 3, 2}};
        System.out.println("SThe compression array:");
        print2Array(compression);
    }

    public static void print2Array(int[][] array) {//打印二维数组的方法
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + " ");
                if (j == array[i].length - 1) {
                    System.out.println();
                }
            }
        }
    }
}

package Demoxx;

public class Demo02 {
    public static void main(String[] args) {
        int[][] array = generateArray(11,11);
        System.out.println("Sparse array:");
        print2Array(array);
        System.out.println("The compression array:");
        int[][] arrayCom = generateCompression(array);
        print2Array(arrayCom);
        System.out.println("The restored sparse array");
        print2Array(regeneratArray(arrayCom));
    }

    public static int[][] generateArray(int line, int column) {
        int[][] array = new int[line][column];
        for (int i = 0; i < array.length; i++) {//产生这个稀疏矩阵
            for (int j = 0; j < array[i].length; j++) {
                if ((((i != 1) && (j != 2))) && (((i != 2) && (j != 3)))) {
                    array[i][j] = 0;
                } else if ((i == 1) && (j == 2)) {
                    array[i][j] = 1;
                } else if ((i == 2) && (j == 3)) {
                    array[i][j] = 2;
                }
            }
        }
        return array;
    }

    public static int[][] generateCompression(int[][] array) {
        //获取稀疏数组里面的有效元素个数
        int effectiveNum = 0;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                if (array[i][j] != 0) {
                    effectiveNum++;
                }
            }
        }
        //产生稀疏数组的压缩数组
        int count = 0;
        int[][] arrayCom = new int[effectiveNum + 1][3];
        arrayCom[0][0] = array.length;
        arrayCom[0][1] = array[1].length;
        arrayCom[0][2] = effectiveNum;
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                if (array[i][j] != 0) {
                    count++;
                    arrayCom[count][0] = i;
                    arrayCom[count][1] = j;
                    arrayCom[count][2] = array[i][j];
                }
            }
        }
        return arrayCom;
    }

    public static void print2Array(int[][] array) {//打印二维数组的方法
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.print(array[i][j] + "  ");
                if (j == array[i].length - 1) {
                    System.out.println();
                }
            }
        }
    }

    public static int[][] regeneratArray(int[][] arrayCom) {//还原这个稀疏数组
        int[][] array = new int[arrayCom[0][0]][arrayCom[0][1]];
        for (int i = 1; i < arrayCom.length; i++) {
            array[arrayCom[i][0]][arrayCom[i][1]] = arrayCom[i][2];
            array[arrayCom[i][0]][arrayCom[i][1]] = arrayCom[i][2];
        }
        return array;
    }
}

成功还原原稀疏数组

自学java,请多多指教!
原文地址:https://www.cnblogs.com/fanfada/p/13765503.html