数据结构(稀疏数组 IO)笔记

初始化二维数组

        //初始化棋盘
        int[][] data = new int[11][11];
        data[4][5] = 1;
        data[5][6] = 2;
        data[0][1] = 2;

 将二维数组转为稀疏数组

    //转为稀松
    public static int[][] toSparseArray(int[][] data) throws IOException {
        //1统计 数据量
        int count = 0;
        for (int[] i : data) {
            for (int i1 : i) {
                if (i1 != 0)
                    count++;
            }
        }
        //2根据数据量 创建稀松数组 并初始化首行 为原数组 行、列、数据量
        int[][] sparseArray = new int[count + 1][3];
        sparseArray[0][0] = data.length;
        sparseArray[0][1] = data[0].length;
        sparseArray[0][2] = count;
        //3遍历数据源记录数据的 坐标
        int row = 0;
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] != 0) {
                    row = count--;
                    sparseArray[row][0] = i;
                    sparseArray[row][1] = j;
                    sparseArray[row][2] = data[i][j];
                }
            }
        }
        System.out.println("-------------sparseArray--------------");
        return sparseArray;
    }

将稀疏数组变回二维数组

        //将稀疏数组转为原数据
    public static int[][] bySparseArray(int[][] sparseArray) throws IOException,         ClassNotFoundException {
        int[][] back = new int[sparseArray[0][0]][sparseArray[0][1]];
        for (int i = 1; i < sparseArray.length; i++) {
            back[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
        }
        System.out.println("-------------back-----------------");
        forEachOut(back);
        return back;
    }

加入 IO 读写

        通过IO流实现文件写入

        //通过 IO 输出到文件
        String dir = new File("").getCanonicalPath();
        String file = dir + "\\src\\main\\resources\\static\\file";
        File path = new File(file);
        if (!path.exists()){
            boolean mkdirs = path.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(path+"/sparseArray1.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(sparseArray);
        fos.close();
        oos.close();

        通过IO流读取文件

        //通过 IO 读取稀松列表
        String dir = new File("").getCanonicalPath();
        String file = dir + "\\src\\main\\resources\\";
        File path = new File(file + pathRelativeToResource);
        FileInputStream fis = new FileInputStream(path);
        ObjectInputStream ois = new ObjectInputStream(fis);
        int[][] sparseArray = (int[][]) ois.readObject();
        fis.close();
        ois.close();

完整代码

import java.io.*;

public class sparseArray {
    public static void main(String[] args) throws IOException, ClassNotFoundException {
        //初始化棋盘
        int[][] data = new int[11][11];
        data[4][5] = 1;
        data[5][6] = 2;
        data[0][1] = 2;
        //遍历棋盘
        forEachOut(data);
        //转为稀松数组
        toSparseArray(data);
        //读取稀松
        String path = "static/file/sparseArray1.txt";
        int[][] ints = bySparseArray(path);
    }

    //遍历棋盘
    public static void forEachOut(int[][] data) {
        for (int[] datum : data) {
            for (int i : datum) {
                System.out.print(i + "  ");
            }
            System.out.println();
        }
    }

    //转为稀松
    public static int[][] toSparseArray(int[][] data) throws IOException {
        //1统计 数据量
        int count = 0;
        for (int[] i : data) {
            for (int i1 : i) {
                if (i1 != 0)
                    count++;
            }
        }
        //2根据数据量 创建稀松数组 并初始化首行 为原数组 行、列、数据量
        int[][] sparseArray = new int[count + 1][3];
        sparseArray[0][0] = data.length;
        sparseArray[0][1] = data[0].length;
        sparseArray[0][2] = count;
        //3遍历数据源记录数据的 坐标
        int row = 0;
        for (int i = 0; i < data.length; i++) {
            for (int j = 0; j < data[i].length; j++) {
                if (data[i][j] != 0) {
                    row = count--;
                    sparseArray[row][0] = i;
                    sparseArray[row][1] = j;
                    sparseArray[row][2] = data[i][j];
                }
            }
        }
        System.out.println("-------------sparseArray--------------");
        forEachOut(sparseArray);
        //通过 IO 输出到文件
        String dir = new File("").getCanonicalPath();
        String file = dir + "\\src\\main\\resources\\static\\file";
        File path = new File(file);
        if (!path.exists()){
            boolean mkdirs = path.mkdirs();
        }
        FileOutputStream fos = new FileOutputStream(path+"/sparseArray1.txt");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(sparseArray);
        fos.close();
        oos.close();
        return sparseArray;
    }

    //读取稀松
    public static int[][] bySparseArray(String pathRelativeToResource) throws IOException, ClassNotFoundException {
        //通过 IO 读取稀松列表
        String dir = new File("").getCanonicalPath();
        String file = dir + "\\src\\main\\resources\\";
        File path = new File(file + pathRelativeToResource);
        FileInputStream fis = new FileInputStream(path);
        ObjectInputStream ois = new ObjectInputStream(fis);
        int[][] sparseArray = (int[][]) ois.readObject();
        fis.close();
        ois.close();
        //将稀疏数组转为原数据
        int[][] back = new int[sparseArray[0][0]][sparseArray[0][1]];
        for (int i = 1; i < sparseArray.length; i++) {
            back[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
        }
        System.out.println("-------------back-----------------");
        forEachOut(back);
        return back;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值