数据结构1 稀疏数组

文章目录

package com.scy.sparsearray;

import java.io.*;

public class SparseArray {
    /**
     * 数据结构:稀疏数组对应五子棋
     * @param args
     */
    public static void main(String[] args) throws FileNotFoundException {
        //创建一个原始的二维数组11*11
        //0表示没有棋子,1表示黑子 2表示蓝子
        int chessArr1[][]  = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        //输出原始数组
        System.out.println("原始的二维数组");
        for (int[] row: chessArr1){
            for (int data: row){
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }
        //将二维数组 转 稀疏数组 的思路
        //1.先遍历二维数组,得到非0数据的个数
        int sum = 0;
        for (int i=0;i<chessArr1.length;i++){
            for (int j=0;j<chessArr1[i].length;j++){
                if (chessArr1[i][j] !=0){
                    sum++;
                }
            }
        }
        System.out.println(sum);
        //2.创建对应的稀疏数组
        int sparseArr[][] = new int[sum+1][3];
        //给稀疏数组赋值
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;
        // 遍历二维数组,将非0的值存放到稀疏数组
        int count = 0; //用于记录是第几个非零数据
        for (int i=0;i<chessArr1.length;i++){
            for (int j=0;j<chessArr1[i].length;j++){
                if (chessArr1[i][j] !=0){
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr1[i][j];
                }
            }
        }
        //输出稀疏数组的形式
        System.out.println();
        System.out.println("得到的稀疏数组为----");
        for (int i=0;i< sparseArr.length;i++){
            System.out.printf("%d\t%d\t%d\t\n",sparseArr[i][0],sparseArr[i][1],sparseArr[i][2]);
        }
        
        //稀疏数组持久化写入磁盘
        System.out.println("稀疏数组持久化写入磁盘");
        FileWriter fileWriter=null;
        try {
            fileWriter= new FileWriter(new File("D:\\sparseArray.data"));
            for (int[] row : sparseArr) {
                fileWriter.write(row[0]+"\t"+row[1]+"\t"+row[2]);
                fileWriter.write("\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        //从磁盘恢复稀疏数组,并转换成为二维数组

        BufferedReader fileReader = new BufferedReader(new FileReader("D:\\sparseArray.data"));
        boolean isNotRead=false;
        int[][] sparseArray2 = null;
        try {
            String lineStr=null;
            int curCount=0;
            while((lineStr=fileReader.readLine())!=null) {
                String[] tempStr=lineStr.split("\t");
                if (!isNotRead){
                    sparseArray2=new int[Integer.parseInt(tempStr[2])+1][3];
                    sparseArray2[curCount][0]=Integer.parseInt(tempStr[0]);
                    sparseArray2[curCount][1]=Integer.parseInt(tempStr[1]);
                    sparseArray2[curCount][2]=Integer.parseInt(tempStr[2]);
                    curCount++;
                    isNotRead=true;
                }else {
                    sparseArray2[curCount][0]=Integer.parseInt(tempStr[0]);
                    sparseArray2[curCount][1]=Integer.parseInt(tempStr[1]);
                    sparseArray2[curCount][2]=Integer.parseInt(tempStr[2]);
                    curCount++;
                }
            }
            System.out.println("从磁盘恢复稀疏数组");
            for (int[] row: sparseArray2){
                for (int data: row){
                    System.out.printf("%d\t",data);
                }
                System.out.println();
            }


        }catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                fileReader.close();
            }catch (IOException e){
                e.printStackTrace();
            }
        }



    /**
         *
         * 稀疏数组转原始的二维数组的思路
         *
         * 1. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,比如上面的  chessArr2 = int [11][11]
         * 2. 在读取稀疏数组后几行的数据,并赋给 原始的二维数组 即可.
         */

        int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];

        for (int i =1;i<sparseArr.length;i++){
            chessArr2[sparseArr[i][0]][sparseArr[i][1]]=sparseArr[i][2];
        }


        //输出后的二维数组
        System.out.println("恢复后的二维数组");
        for (int[] row : chessArr2) {
            for (int data : row) {
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值