JAVA数据结构之稀疏数组

稀疏数组
例子
在这里插入图片描述

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

系数数组的处理方法
(1)记录数组一共有几行几列,有多少个不同的值
(2)把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模

案例
[0]第一行记录数组有几行几列,第三列是数组中总共有几个不同的值
[1]-[8]几行几列所对应的值为多少
在这里插入图片描述

思路分析
在这里插入图片描述

代码实现:

package my.sust;
public class Chess {
       public static void main(String[] args) {
             // TODO Auto-generated method stub
             int[][] chessary=new int[11][11];
             chessary[1][2]=1;
             chessary[2][3]=2;
             chessary[4][5]=1;
             System.out.println("原始的二维数组");
             for(int[] row :chessary) {
                    for(int data : row) {
                           System.out.printf("%d\t",data);
                    }
             System.out.println();
             }
             //棋盘转换为稀疏数组
             int a[][] = sparsearray(chessary);
             for(int[] row :a) {
                    for(int data : row) {
                           System.out.printf("%d\t",data);
                    }
             System.out.println();
             }
             int b[][] = sparse(a);
             for(int[] row :b) {
                    for(int data : row) {
                           System.out.printf("%d\t",data);
                    }
             System.out.println();
             }
       }
       public static int[][] sparsearray(int[][] chessary){//棋盘转换为稀疏数组
             int sum = 0;
             for(int i=0;i<chessary.length;i++) {
                    for(int j=0;j<chessary[0].length;j++) {
                           if(chessary[i][j] != 0) {
                                 sum++;
                           }
                    }
             }
             int[][] array = new int[sum+1][3];
             
             array[0][0] = chessary.length;//读取棋盘的行row
             array[0][1] = chessary[0].length;//读取棋盘的列co
             array[0][2] = sum;
             int count = 0;//计数器作用,有一次非0的值,就用count作为array中的行号
             for(int i=0;i<chessary.length;i++) {
                    for(int j=0;j<chessary[0].length;j++) {
                           if(chessary[i][j] != 0) {
                                 count++;
                                 array[count][0] = i;
                                 array[count][1] = j;
                                 array[count][2] = chessary[i][j];
                           }
                    }
             }
             return array;
       }
       public static int[][] sparse(int[][] array){
             int row = array[0][0];
             int co = array[0][1];
             int sum = array[0][2];
             int[][] chessary = new int[row][co];
             for(int i=1;i<row;i++) {
                    for(int j=0;j<co;j++) {
                           chessary[i][j] = 0;
                    }
             }
             for(int i=1;i<sum+1;i++) {
                    for(int j=0;j<3;j++) {
                           int x = array[i][0];
                           int y = array[i][1];
                           chessary[x][y] = array[i][2];
                    }
             }
             
             return chessary;
       }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值