数据结构之稀疏数组

五子棋问题

在这里插入图片描述
在这里插入图片描述

什么是稀疏数组

稀疏数组可以看做是普通数组的压缩,但是这里说的普通数组是值无效数据量远大于有效数据量的数组。
在这里插入图片描述
如下:
原始数组:

              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 0 3 0 0 0 0 0 0
              0 0 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]  11 11 3
         [1]  1  2  1
         [2]  2  4  3
         [3]  4  2  2

用稀疏数组来解决五子棋问题

二维数组=>稀疏数组=>写入文件

 public static void main(String[] args) throws IOException{
        int arr[][] =new int[10][10];//创建一个10*10的普通二维数组表示10*10的棋盘
        arr[2][3]=1;//1表示黑子
        arr[3][4]=2;//2表示白子
        arr[4][5]=2;//2表示白子
        arr[3][3]=1;//1表示黑子
        System.out.println("-----------------普通数组遍历---------------------");
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                System.out.print("\t"+arr[i][j]);
            }
            System.out.println();
        }
        System.out.println("-----------------构造成稀疏数组---------------------");
        //找到普通数组中一共有几个非0数字
        int num=0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
               if(arr[i][j]>0)
                   num++;
            }
        }
        System.out.println("num="+num);
        //定义稀疏数组行数等于num+1,列数固定为3
        int sparse[][] =new int[num+1][3];
        sparse[0][0]=10;
        sparse[0][1]=10;
        sparse[0][2]=num;
        int count=0;//代表行数
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                if(arr[i][j]!=0){
                    count++;
                    sparse[count][0]=i;
                    sparse[count][1]=j;
                    sparse[count][2]=arr[i][j];
                }
            }
        }
        System.out.println("-----------------稀疏数组遍历---------------------");
        for (int i = 0; i < sparse.length; i++) {
            System.out.println(sparse[i][0]+"\t"+sparse[i][1]+"\t"+sparse[i][2]);
        }

        System.out.println("将稀疏数组存入文件");
        String path="e:/data.txt";
        File file = new File(path);
        if (!file.isFile()) {
            //判断磁盘下是否有改文件,没有则创建
            file.createNewFile();
        }
        FileOutputStream outputStream = new FileOutputStream(file);//文件输出流写进文件
        for (int i = 0; i < sparse.length; i++) {
            for (int j = 0; j < 3; j++) {
                //将数组写入文件
                outputStream.write(sparse[i][j]);
            }
        }
        outputStream.close();
        System.out.println("存盘成功");
    }

测试结果:

-----------------普通数组遍历---------------------
	0	0	0	0	0	0	0	0	0	0
	0	0	0	0	0	0	0	0	0	0
	0	0	0	1	0	0	0	0	0	0
	0	0	0	1	2	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
-----------------构造成稀疏数组---------------------
num=4
-----------------稀疏数组遍历---------------------
10	10	4
2	3	1
3	3	1
3	4	2
4	5	2
将稀疏数组存入文件
存盘成功

读取文件=>稀疏数组=>二维数组

public static void main(String[] args) throws IOException {
        //从data.txt读取文件
        String path="e:/data.txt";
        File file = new File(path);
        //文件输入流
        FileInputStream inputStream = new FileInputStream(file);
        //定义字节数组用来接收数据
        byte bytes[]= new byte[128];
        //将文件内容读入字节数组,并且得到字符的长度
        int length = inputStream.read(bytes);
        //得到稀疏数组的行数,因为列数是固定的为3
        int row =length/ 3;
        //定义稀疏数组
        int sparse[][]=new int[row][3];
        System.out.println(" 遍历字节数组");
        for (int i = 0; i < length; i++) {
            System.out.print(bytes[i]+"\t");
        }
        //将字节数组中的数字存入稀疏数组
        int k=0;
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                sparse[i][j]=bytes[k];
                k++;
            }
        }
        System.out.println();
        System.out.println("遍历稀疏数组");
        for (int i = 0; i < sparse.length; i++) {
            System.out.println(sparse[i][0]+"\t"+sparse[i][1]+"\t"+sparse[i][2]);
        }
        //将稀疏数组转为普通二维数组
        int[][] arr=new int[sparse[0][0]][sparse[0][1]];
        for (int i = 1; i < sparse.length; i++) {
            arr[sparse[i][0]][sparse[i][1]]=sparse[i][2];
        }
        System.out.println("二维数组遍历");
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                System.out.print("\t"+arr[i][j]);
            }
            System.out.println();
        }
    }

测试结果:

遍历稀疏数组
10	10	4
2	3	1
3	3	1
3	4	2
4	5	2
二维数组遍历
	0	0	0	0	0	0	0	0	0	0
	0	0	0	0	0	0	0	0	0	0
	0	0	0	1	0	0	0	0	0	0
	0	0	0	1	2	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

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

活跃的咸鱼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值