二维数组转换为稀疏数组以及在磁盘的存取

public class SparseArray {
    public static void main(String[] args) {
        //二维数组和稀疏数组的转化
        //创建一个11*11的棋盘,原始二维数组
        //0:没有棋子 1:表示黑子 2:表示白子
        int[][] chessArr1 = new int[11][11];
        int sum = 0;
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;

        //输出原始数组,记录非0数字个数
        System.out.println("原始二维数组");
        for (int [] data : chessArr1){
            for (int da : data){
                System.out.printf("%d ",da);
                if (da != 0){
                    sum++;
                }
            }
            System.out.println();
        }
        //System.out.println(sum);
        //创建对应的稀疏数组
        int[][] sparseArr = new int[sum + 1][3];
        int count = 0;
        sparseArr[0][0] = chessArr1.length;
        sparseArr[0][1] = chessArr1[0].length;
        sparseArr[0][2] = sum;
        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("稀疏数组为:");
        for (int [] data : sparseArr){
            for (int da : data){
                System.out.printf("%d ",da);
            }
            System.out.println();
        }

        File f = new File("./dataStructure/chess.txt");
        if (!f.exists()){
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            FileWriter fw = new FileWriter(f);
            for (int [] data : sparseArr){
                for (int da : data){
                    fw.write(da+ " ");
                }
                fw.write("\n");
            }
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }


        //将稀疏数组从文件中读取出并恢复成二维数组
        //稀疏数组如下
        //11 11 2  为二维数组行列以及不为零的元素数
        //1  2  1  不为零元素位置
        //2  3  2
        try {
            InputStreamReader isr = new InputStreamReader(new FileInputStream(f));
            BufferedReader br = new BufferedReader(isr);
            String line1 = null;
            if ((line1 = br.readLine()) != null){
                String[] line = line1.split(" ");
                int[][] chessArr2 = new int[Integer.valueOf(line[0])][Integer.valueOf(line[1])];
                int rowCount = Integer.valueOf(Integer.valueOf(line[2]));
                while(rowCount > 0){
                    line = br.readLine().split(" ");
                    chessArr2[Integer.valueOf(line[0])][Integer.valueOf(line[1])] = Integer.valueOf(line[2]);
                    rowCount--;
                }

                System.out.println("恢复二维数组");
                for (int [] data : chessArr2){
                    for (int da : data){
                        System.out.printf("%d ",da);
                    }
                    System.out.println();
                }
                br.close();
                isr.close();
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
原始二维数组
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 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 0 
稀疏数组为:
11 11 2 
1 2 1 
2 3 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 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 0 

Process finished with exit code 0

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值