稀疏数组一 数组转稀疏数组 稀疏数组恢复

当一个数组在中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。

稀疏数组的处理方法;

  1. 记录数组一共有几行几列,有多少个不同的值。
  2. 把具有不同值的元素的行列有值记录在一个小规模的数组中,从而缩小程序的规模。

由7x6的数组转换成3x9的稀疏数组

package jiegou;

import javax.management.relation.Role;
import javax.swing.*;

public class SparseArray {
    public static void main(String[] args) {
        // 创建一个原始的二维数组 11*11
        // 0 表示没有 1 黑子 2 蓝子

        int row = 11;
        int col = 11;

        Sparse sparse = new Sparse();
        int[][] chessArr = sparse.createArray(row,col);
        System.out.println("原始的数组");
        sparse.printArray(chessArr);


        int[][] sparseArr = sparse.arrayToSparse(chessArr);
        // 输出
        System.out.println("稀疏数组数组");
        sparse.printArray(sparseArr);

        // 转行回原始数组
        int[][] oldArr = sparse.sparseToArray(sparseArr);
        // 输出
        System.out.println("还原后的数组");
        sparse.printArray(oldArr);
    }
}

class Sparse
{

    // 创建一个原始数组
    public int[][] createArray(int row,int col)
    {
        int[][] array = new int[row][col];
        array[1][2] = 1;
        array[2][3] = 2;
        array[3][4] = 2;
        return array;
    }

    // 打印二维数组
    public void printArray(int[][] array)
    {
        for(int[] item : array) {
            for(int data : item) {
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }
    }

    // 数组转为稀疏数组
    public int[][] arrayToSparse(int[][] chessArr)
    {
        // 遍历原始数组得到有效的非 0 个数
        int sum = 0,row = 0,col=0;
        row = chessArr.length;
        col = chessArr[0].length;

        for(int[] item : chessArr) {
            for(int data : item) {
                if(data != 0 ){
                    sum++;
                }
            }
        }
        System.out.printf("sum=%d\n",sum);

        // 创建对应的稀疏数组 第三行3是固定的 第一个二个存几行几列  所以行数是个数+1(sum+1)
        int[][] sparseArr = new int[sum+1][3];
        // 给稀疏数组赋值
        sparseArr[0][0] = row; // 总行数
        sparseArr[0][1] = col; // 总列数
        sparseArr[0][2] = sum; // 总数
        //遍历二维数组 将非 0 的值存入到稀疏数组
        // 设置一个计数器 从第二行开始
        int count = 1;
        for(int i=1;i<row;i++){
            for(int j=0;j<col;j++){
                if(chessArr[i][j] != 0) {
                    sparseArr[count][0] = i; // 这个非 0 所在的行
                    sparseArr[count][1] = j; // 这个非 0 所在的列
                    sparseArr[count][2] = chessArr[i][j]; //值
                    count++;
                }
            }
        }

        return sparseArr;
    }

    // 稀疏数组转为正常数组  第一行的第一列是行数 第二个是列数 第三个是非 0 的总数
    public int[][] sparseToArray(int[][] array)
    {
        int row ,col;
        row = array[0][0];
        col = array[0][1];

        int[][] arr = new int[row][col];

        if(array[0][2] > 0) {
            for(int i=1;i< array.length;i++){
                for (int j=0;j<3;j++){
                    arr[array[i][0]][array[i][1]] = array[i][2]; // 第三个是值
                }
            }
        }

        return arr;
    }
}


/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/bin/java -javaagent:/Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar=65502:/Applications/IntelliJ IDEA.app/Contents/bin -Dfile.encoding=UTF-8 -classpath /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home/lib/tools.jar:/Users/mac/www/java/shujujiegou/out/production/shujujiegou jiegou.SparseArray
原始的数组
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	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	
sum=3
稀疏数组数组
11	11	3	
1	2	1	
2	3	2	
3	4	2	
还原后的数组
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	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	

Process finished with exit code 0


posted @ 2021-08-14 00:18  brady-wang  阅读(47)  评论(0编辑  收藏  举报