java 稀疏数组和二维数组转换,并保存稀疏数组到文件后可以读取

稀疏数组和二维数组转换

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

  • 稀疏数组的处理方法:

    • 记录数组一共有多少行,有多少个不同的值
    • 把具有不同值得元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模
  • 应用:

    • 保存二维数组(棋盘、地图)
  1. 二维数组 ——> 稀疏数组的思路
    1. 遍历原始二维数组,得到有效数据的个数sum
    2. 根据sum创建稀疏数组SparseArr int[sum + 1][3]
    3. 将二维数组的有效数据存入到稀疏数组
  2. 稀疏数组 ——> 二维数组
    1. 读取稀疏数组的第一行,根据第一行的数据,创建原始二维数组,比如上面的chessArr2 = int[11][11]
    2. 在读取稀疏数组后几行的数据,并赋给原始的二维数组即可
package com.starking.sparseArr;

/**
 * @author Manix
 * 稀疏数组和原始二维数组的相互转换写入文件,保存和读取
 */

public class SparseArray {
    public static void main(String[] args) {
        //创建一个二维数组 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 item : row) {
                System.out.printf("%d\t", item);
            }
            System.out.println();
        }

        //将二维数组 转 稀疏数组的思路
        //先遍历二维数组,得到非0个元素个数
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (chessArr1[i][j] != 0) {
                    sum++;
                }
            }
        }
        System.out.println("有效元素的数量Sum为:" + sum);

        //2.创建对应的稀疏数组
        int[][] sparseArr = new int[sum + 1][3];
        //给稀疏数组赋值
        sparseArr[0][0] = 11;
        sparseArr[0][1] = 11;
        sparseArr[0][2] = sum;

//         给稀疏数组赋值
        int count = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; 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]);
        }

        //将稀疏数组----》---恢复成原始的二维数组
        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();

        //输出恢复之后的二维数组
        System.out.println("稀疏数组----》---恢复的二维数组:");
        for (int[] row : chessArr2) {
            for (int item : row) {
                System.out.printf("%d\t", item);
            }
            System.out.println();
        }
    }
    
    
    
    
    
    
     /**
     * 添加保存稀疏数组和从本地文件读取稀疏数组的功能
     */
    
//    创建内部类---保存和读取稀疏数组工具类
    public static class SaveAndReadUtil {

        //      写入文件的方法
        public static void saveArray(int[][] array, String path) {
//            创建文件输入流
            FileWriter fw = null;
            try {
//                写入的文件路径及文件名
                File file = new File(path);
                if (!file.exists()) {
                    file.createNewFile();
                }
                //把文件写入到字节流中
                fw = new FileWriter(file);
                //循环遍历数组写入
                for (int[] ints : array) {
                    for (int j = 0; j < array.length - 1; j++) {
                        fw.write(ints[j] + "\t");
                    }
                    //数组的最后一行不加"\t"
                    fw.write(ints[array[0].length - 1] + "");
                    fw.write("\n");
                }
                //刷新流
                fw.flush();

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    //如果fw不为空,就将其关闭
                    if (fw != null) {
                        fw.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }


        //读取数组文件的方法
        public static int[][] readArray(String path) {
            //声明字符输入流
            FileReader fr = null;
            //声明字符输入缓冲流
            BufferedReader br = null;
            //定义一个二维数组
            int[][] sparseArr0 = null;

            try {
                fr = new FileReader(path);
                //通过BufferReader包装字符输入流
                br = new BufferedReader(fr);
                //穿件一个集合用来存读取的文件数据
                List<String> list = new ArrayList<>();
                //用来存放一行的数据
                String lineStr;
                //逐行读取文件内容
                while ((lineStr = br.readLine()) != null) {
                    list.add(lineStr);
                }
                //获取文件的行数
                int lineNum = list.size();
                //获取文件列数
                String s = list.get(0);
                int columnNum = s.split("\t").length;
                //根据文件行数和列数创建对应的数组
                sparseArr0 = new int[list.size()][columnNum];
                //记录输出当前行数
                int count = 0;
                //遍历集合,将集合中的数据存入数组中
                for (String str : list) {
                    //读取str按照“\t”分割,用字符串数组接收
                    String[] strs = str.split("\t");
                    for (int i = 0; i < columnNum; i++) {
                        sparseArr0[count][i] = Integer.parseInt(strs[i]);
                    }
                    count++;
                }

            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                //关闭字符输入流
                try {
                    if (fr != null) {
                        fr.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if (br != null) {
                        br.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            //返回稀疏数组
            return sparseArr0;
        }
    }
       
    
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

star__king

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

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

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

打赏作者

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

抵扣说明:

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

余额充值