1、稀疏数组

143 阅读2分钟
稀疏数组实际上还是一个数组,他是对数组的简化。在对一些数据进行保存的时候,由于有很多无用的的数据,而且这些无用的数据占据整个数组个数的一半以上,浪费空间,而且当我们对其存入磁盘等操作的时候,会增加IO的读写量
  • 棋盘保存 image.png 对其进行存储的时候,使用二维数组进行保存 image.png 但是这极大的消耗内存,我们引入二维数组进行保存

  • 稀疏数组

    • 它为3列,有效字段+1行。
    • 第一行保存原数组的规模,第一行第一列保存原二维数组的行数,第二列保存原二维数组的列数,第三列保存原二维数组的有效数据
    • 其余行保存有效数据的信息,其余行的第一列都保存的为有效数据所在的行,第二列保存有效数据所在的列,第三行保存有效数据的值
  • 代码实现

public class LitGrassSparseArray<T> {  
  
    //原数组转稀疏数组  
    public static Object[][]  arrayToSparse(Object[][] initArray){  
        int count = 0;  
        for (int i = 0; i < initArray.length; i++) {  
            for (int j = 0; j < initArray.length; j++) {  
                if(!Objects.isNull(initArray[i][j])){  
                    count++;  
                }  
            }  
        }  
  
        Object[][] sparseArray = new Object[count+1][3];  
        sparseArray[0][0] = initArray.length;  
        sparseArray[0][1] = initArray[0].length;  
        sparseArray[0][2] = count;  
  
        int index = 1;  
        for (int i = 0; i < initArray.length; i++) {  
            for (int j = 0; j < initArray.length; j++) {  
                if(!Objects.isNull(initArray[i][j])){  
                    sparseArray[index][0] = i;  
                    sparseArray[index][1] = j;  
                    sparseArray[index][2] = initArray[i][j];  
                    index++;  
                }  
            }  
        }  
  
        return sparseArray;  
    }  
  
  
    //稀疏数组转原数组  
    public static Object[][] sparseToArray(Object[][] sparseArray){  
        Object[][] initArray = new Object[(Integer) sparseArray[0][0]][(Integer) sparseArray[0][1]];  
        for (int i = 1; i < sparseArray.length; i++) {  
            initArray[(Integer) sparseArray[i][0]][(Integer) sparseArray[i][1]] = sparseArray[i][2];  
        }  
        return initArray;  
    }  
  
    //保存到磁盘  
    public static void persverSparse(Object[][] sparseArray) throws Exception {  
        Props props = PropsUtil.get("system.properties");  
        Path path = Paths.get((String) props.get("litGrass.path.file"),"稀疏数组", "persverSparse.txt");  
        PathUtil.mkParentDirs(path);  
        RandomAccessFile randomAccessFile = FileUtil.createRandomAccessFile(path,FileMode.rw);  
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();  
        ObjectOutputStream oos = new ObjectOutputStream(byteArrayOutputStream);  
        oos.writeObject(sparseArray);  
        randomAccessFile.seek(0);  
        randomAccessFile.write(byteArrayOutputStream.toByteArray());  
        oos.close();  
        byteArrayOutputStream.close();  
        randomAccessFile.close();  
    }  
  
    //从磁盘读取  
    public static Object[][] readSparse() throws Exception{  
        Props props = PropsUtil.get("system.properties");  
        Path path = Paths.get((String) props.get("litGrass.path.file"),"稀疏数组", "persverSparse.txt");  
        RandomAccessFile randomAccessFile = FileUtil.createRandomAccessFile(path,FileMode.rw);  
        byte[] bytes = new byte[(int) randomAccessFile.length()];  
        randomAccessFile.read(bytes);  
  
        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes));  
        Object[][] o = (Object[][]) ois.readObject();  
        ois.close();  
        randomAccessFile.close();  
        return o;  
    }  
  
    public static void main(String[] args) throws Exception {  
        Integer chessArr1[][] = new Integer[11][11];  
        chessArr1[1][2] = 1;  
        chessArr1[3][6] = 2;  
        persverSparse(arrayToSparse(chessArr1));  
        Object[][] sparseArray = readSparse();  
        for (Object[] objects : sparseArray) {  
            for (Object object : objects) {  
                System.out.print(object+"\t");  
            }  
            System.out.println();  
        }  
        System.out.println("***************");  
        for (Object[] objects : sparseToArray(sparseArray)) {  
            for (Object object : objects) {  
                if(Objects.isNull(object)){  
                    System.out.print(0+"\t");  
                    continue;  
                }  
                System.out.print(object+"\t");  
            }  
            System.out.println();  
        }  
    }  
}