二维数组与稀疏数组互转并入库的全过程

最近在学习数据结构与算法,按照视频教程,自己跟着写一遍巩固记忆。

1.二维数组转稀疏数组并写入磁盘

public static void main(String[] args) {
        //测试稀疏数组
        //arrayChar1 棋盘,1代表黑子,2代表白子
        int arrayChar1[][] = new int[11][11];
        arrayChar1[1][2] = 1;
        arrayChar1[2][3] = 2;

        for (int row[]:arrayChar1) {
            for (int data :row) {
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }

        //二维数组转稀疏数组
        //1.遍历二维数组里面不等于0的个数
        int sum = 0;
        for (int row[]:arrayChar1) {
            for (int data :row) {
                if (data != 0) {
                    sum++;
                }
            }
        }
        //2.创建稀疏数组,稀疏数组的行数为sum+1,列为3
        int count = 0;
        int spareArr[][] = new int [sum+1][3];
        spareArr[0][0] = arrayChar1.length;
        spareArr[0][1] = arrayChar1[0].length;
        spareArr[0][2] = sum;

        for(int i = 0; i < arrayChar1.length ; i++){
            for(int j = 0;j < arrayChar1[i].length;j++){
                if (arrayChar1[i][j] != 0) {
                    count++;
                    //
                    spareArr[count][0]=i;
                    //
                    spareArr[count][1]=j;
                    //
                    spareArr[count][2]=arrayChar1[i][j];
                }
            }
        }
        //输出稀疏数组
        for (int row[]:spareArr) {
            for (int data :row) {
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }

        //入磁盘
        FileWriter writer = null;
        try {
            writer = new FileWriter("D:/opt/test/map.data");
            for (int row[]:spareArr) {
                for (int data :row) {
                    writer.write(String.valueOf(data)+" ");
                }
                //写入文件换行
                writer.write("\r\n");
            }
            writer.flush();

            if (writer!=null) {
                writer.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

2.从磁盘读取转稀疏数组,再转二维数组全过程

public static void main(String[] args) {
        //从磁盘中读数据
        try {
            FileReader fileReader = new FileReader("D:/opt/test/map.data");
            BufferedReader br = new BufferedReader(fileReader);
            try {
                //读取第一行
                String s = br.readLine();
                String[] split = s.split(" ");
                //输出结果11 11 2
                //创建稀疏数组
                int spareArray[][] = new int[Integer.valueOf(split[2])+1][3];
                for (int i = 0;i < split.length ;i++) {
                    spareArray[0][i] = Integer.valueOf(split[i]);
                }
                //按行读取
                int count = 1;
                while ((s = br.readLine()) != null){
                    split = s.split(" ");
                    for (int i = 0;i<split.length;i++) {
                        spareArray[count][i] = Integer.valueOf(split[i]);
                    }
                    count++;
                }

                //输出稀疏数组
                for (int row[]:spareArray) {
                    for (int data :row) {
                        System.out.printf("%d\t",data);
                    }
                    System.out.println();
                }
                //将稀疏数组转为二维数组
                int array[][] = new int[spareArray[0][0]][spareArray[0][1]];

                for(int i = 1; i < spareArray.length ; i++){
                        array[spareArray[i][0]][spareArray[i][1]] = spareArray[i][2];
                }
                //输出二维数组
                for (int row[]:array) {
                    for (int data :row) {
                        System.out.printf("%d\t",data);
                    }
                    System.out.println();
                }



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

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

    }

 

posted @ 2021-08-26 15:42  小拾柒~  阅读(50)  评论(0编辑  收藏  举报