二维数组转稀疏数组,写入文件后再读取文件,将内容转回二维数组

该方法模拟的是将棋盘的位置保存到稀疏数组中,降低存储的数据量,通过写入磁盘做持久化,再读入后恢复棋盘内容。
package com.moson.sparsearray;

import java.io.*;

/**
 * 数组转稀疏数组再转回数组
 * @author moxingjian
 * @version 1.0
 * @date 10/10/19 7:40 PM
 */
public class SparseArray {
    public static void main(String[] args) {
        // 初始化数组
       	int[][] cheerArray = new int[11][11];
       	// 添加值,添加的是棋盘的位置
        cheerArray[1][2] = 1;
        cheerArray[2][3] = 2;
        cheerArray[3][6] = 1;
        cheerArray[4][4] = 2;
        System.out.println("原始数组~");
       	for (int[] row : cheerArray) {
       	    for (int data : row) {
       	        System.out.printf("%d\t" , data);
            }
            System.out.println();
        }

        // 获取非0的个数
        int sum = 0;
       	for (int i = 0; i < 11; i++) {
       	    for (int j = 0; j < 11; j++) {
       	        if (cheerArray[i][j] != 0) {
       	            sum++;
                }
            }
        }
        // 初始化稀疏数组
        int[][] sparseArray = new int[sum+1][3];
       	// 第一行记录的是:行、列数和非0的个数
        sparseArray[0][0] = 11;
        sparseArray[0][1] = 11;
        sparseArray[0][2] = sum;
        // 记录非0的行数
        int count = 0;
        // 填充稀疏矩阵剩下的行
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (cheerArray[i][j] != 0) {
                    count++;
                    sparseArray[count][0] = i;
                    sparseArray[count][1] = j;
                    sparseArray[count][2] = cheerArray[i][j];
                }
            }
        }


        System.out.println("得到稀疏数组~");
        // 输出稀疏数组
      /*  for (int[] row : sparseArray) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }*/
        for (int i = 0; i < sparseArray.length; i++) {
            System.out.printf("%d\t%d\t%d\t\n", sparseArray[i][0], sparseArray[i][1], sparseArray[i][2]);
        }

        String pathname = "/Users/moxingjian/IdeaProjects/DataStructures/src/main/java/com/moson/sparsearray/saprseArray.txt";
        // 输出到文件中
        File file = new File(pathname);
        FileOutputStream fileOutputStream = null;
        if (!file.exists()) {
            try {
                //创建文件
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        try {
            fileOutputStream = new FileOutputStream(file, true);
            for (int i = 0; i < sparseArray.length; i++) {
                StringBuffer stringBuffer = new StringBuffer();
                stringBuffer.append( sparseArray[i][0] + "\t" +  sparseArray[i][1] + "\t" + sparseArray[i][2] + "\n");
                System.out.println(stringBuffer);
                fileOutputStream.write(stringBuffer.toString().getBytes("utf-8"));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }



        System.out.println("从文件中读取稀疏数组~");
        // 从文件中读取稀疏数组
        File sparseArrayData = new File(pathname);

        int[][] sourceCheerArray = null;
        if (file.exists()) {
            FileReader fileReader = null;
            BufferedReader bufferedReader = null;
            try {
                fileReader = new FileReader(sparseArrayData);
                bufferedReader = new BufferedReader(fileReader);
                String line = null;
                int countSparse = 0;
                int number = 0;
                while ((line = bufferedReader.readLine()) != null) {
                    System.out.println(line);
                    // 将每一行转为数组
                    String[] split = line.split("\t");
                    Integer row = Integer.valueOf(split[0]);
                    Integer col = Integer.valueOf(split[1]);
                    Integer value = Integer.valueOf(split[2]);
                    if (countSparse == 0) {
                        sourceCheerArray = new int[row][col];
                    } else {
                        number++;
                        sourceCheerArray[row][col] =  value;
                    }
                    countSparse++;
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    bufferedReader.close();
                    fileReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }




        // 将稀疏数组转回二维数组
      /*  int[][] sourceCheerArray = new int[sparseArray[0][0]][sparseArray[0][1]];
        // 将稀疏数组中记录的值填充回二维数组
        for (int i = 1; i < sparseArray.length; i++) {
            sourceCheerArray[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
        }*/

        System.out.println("得到原始二维数组~");
        // 输出源二维数组
        for (int[] row : sourceCheerArray) {
            for (int data : row) {
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值