如何将稀疏数组写出到磁盘中

 public static void main(String[] args) {
//        创建一个原始数组
//        0表示没有子,1表示白字,2表示黑子
        int[][] chessArr = new int[11][11];
        int sum = 0;
        chessArr[1][2] = 1;
        chessArr[2][3] = 2;
        chessArr[3][4] = 1;
        System.out.println("原始数组为:");
        for (int[] row : chessArr) {
            for (int data : row) {
                if (data != 0) {
                    sum++;
                }
                System.out.printf("%d\t", data);
            }
            System.out.println();
        }
        System.out.println(sum);
        System.out.println("稀疏数组为:");
        System.out.print("行" + "\t" + "列" + "\t" + "值");
        System.out.println();
        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 < chessArr.length; i++) {
            for (int j = 0; j < chessArr.length; j++) {
                if (chessArr[i][j] != 0) {
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr[i][j];
                }
            }
        }
        for (int[] row : sparseArr) {
            for (int data : row) {
                System.out.print(data + "\t");
            }
            System.out.println();
        }
        FileOutputStream fos = null;
        OutputStreamWriter osw = null;
        try {
            System.out.println("创建/打开文档");
            fos = new FileOutputStream(new File("D:\\xx.txt"));
            osw = new OutputStreamWriter(fos, "utf-8");
            System.out.println("写入中");
            for (int i = 0; i < sparseArr.length; i++) {
                osw.write(String.valueOf(sparseArr[i][0]));
                osw.write(",");
                osw.write(String.valueOf(sparseArr[i][1]));
                osw.write(",");
                osw.write(String.valueOf(sparseArr[i][2]));
                osw.write(",");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (osw != null) {
                try {
                    osw.close();
                } catch (IOException e) {
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        System.out.println("写入完成");
        FileInputStream fis = null;
        InputStreamReader isr = null;
        String str = null;
        try {
            System.out.println("读取文件");
            fis = new FileInputStream(new File("D:\\xx.txt"));
            isr = new InputStreamReader(fis, "utf-8");
            char[] buffer = new char[1024];
            int len;
            while ((len = isr.read(buffer)) != -1) {
                str = new String(buffer, 0, len);
                System.out.println(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (isr != null) {
                try {
                    isr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.println("读取成功");
        //还原为稀疏数组
        String ss = str.toString();
        String[] sb1 = ss.toString().split(",");
        int[][] sp = new int[sb1.length / 3][3];
        int num = 0;
        for (int i = 0; i < sb1.length; i += 3) {
            sp[num][0] = Integer.parseInt(sb1[i]);
            sp[num][1] = Integer.parseInt(sb1[i + 1]);
            sp[num][2] = Integer.parseInt(sb1[i + 2]);
            num++;
        }
        System.out.println("还原稀疏数组");
        System.out.println("行" + "\t" + "列" + "\t" + "值");
        for (int[] row : sp) {
            for (int data : row) {
                System.out.print(data + "\t");
            }
            System.out.println();
        }

//还原为原始数组
        System.out.println("还原为原始数组:");
        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];
        }
        for (int[] row : chessArr2) {
            for (int data : row) {
                System.out.print(data + "\t");
            }
            System.out.println();
        }

    }


给TA打赏
共{{data.count}}人
人已打赏
知识碎片

CSS中元素的层级问题及更改定位元素的层级方法

2021-11-26 21:55:26

知识碎片

垂直对齐方式及常见可以解决的问题

2021-11-27 15:27:17

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧