二维数组转稀疏数组,并保存本地。从本地读取稀疏数组并还原成二维数组。

该博客主要介绍了如何将二维数组转换为稀疏数组,并实现稀疏数组的存储和从本地磁盘读取。内容包括二维数组转稀疏数组、稀疏数组转回二维数组、将稀疏数组存入本地磁盘以及从本地磁盘恢复的过程。测试了整个流程,确保数据的准确性和完整性。
摘要由CSDN通过智能技术生成
import org.junit.Test;

import java.io.*;

public class parse {

    /**
     * 二维数组转稀疏数组
     * @param arr
     * @return
     */
    public  int[][] arrToSparse(int[][] arr){
        long l = System.currentTimeMillis();
        int sum=0;//记录arr有几个需要记录的数

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if(arr[i][j]!=0){
                   sum++;
                }
            }
        }
        //创建稀疏数组
        int[][] sparse = new int[sum+1][3];
        //为稀疏数组第一行赋值
        sparse[0][0]=arr.length;
        sparse[0][1]=arr[0].length;
        sparse[0][2]=sum;
        //用来给sparse换行
        int count=0;
        //为稀疏数组后面赋
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                if(arr[i][j]!=0){
                    count++;
                    sparse[count][0]=i;
                    sparse[count][1]=j;
                    sparse[count][2]=arr[i][j];
                }
            }
        }
        long l1 = System.currentTimeMillis();
        System.out.println("arrToSparse所用时间"+(l1-l));
        return sparse;
    }

    /**
     * 存入本地磁盘
     * @param sparse
     */
    public void saveToLocalDish(int[][] sparse,String location){
        //存入本地磁盘
        FileOutputStream fos = null;
        DataOutputStream dos = null;
        try {
            fos = new FileOutputStream(new File(location));
            dos = new DataOutputStream(fos);
            for (int[] row : sparse){
                for (int data: row){
                    dos.writeInt(data);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(dos!=null){
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fos!=null){
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 从本地磁盘取出
     */
    public int[][] getFromLocalDish(String location){
        int[][] arr={};

        //造流
        FileInputStream fis = null;
        DataInputStream dis = null;

        try {
            fis = new FileInputStream(new File(location));
            dis = new DataInputStream(fis);

            //2.读取数据
            int row = dis.readInt();
            int column = dis.readInt();
            int count = dis.readInt();
            //稀疏数组第一行有三个数字 分别代表原数组的 有几行  有几列  有多少个值
            arr = new int[row][column];
            //根据count 给 arr赋值
            for (int i = 0; i < count; i++) {
                arr[dis.readInt()][dis.readInt()] = dis.readInt();
            }


        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭流资源
            try {
                if(dis!=null)
                dis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if(fis!=null)
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return arr;
    }

    /**
     * 稀疏数组转二位数组
     * @param sparse
     * @return
     */
    public int[][] sparseToArr(int[][] sparse){
        long l = System.currentTimeMillis();
        int row = sparse[0][0];//获取arr的行数
        int col = sparse[0][1];//获取arr的列数
        //创建arr
        int[][] arr = new int[row][col];

        for (int i = 1; i < sparse.length; i++) {
            arr[sparse[i][0]][sparse[i][1]] = sparse[i][2];
        }
        long l1 = System.currentTimeMillis();
        System.out.println("sparseToArr所用时间"+(l1-l));
        return arr;
    }

    /**
     * 二维数组的遍历
     * @param arr
     */
    public  void arrToString(int[][] arr){
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                System.out.printf("%d\t",arr[i][j]);
            }
            System.out.println();
        }
    }

   

    /**
     * 测试是否保存到本地
     * 本地又是否能还原回去
     */
    @Test
    public void test2(){
        //1.初始化二位数组
        int[][] arr = new int[11][11];
        arr[2][3] = 1;
        arr[3][4] = 2;
        //1.初始化 文件需要保存到的位置
        String location = "sparseArray.dat";

        //2.转换为稀疏数组  并且遍历稀疏数组看是否转换成功
        int[][] sparseArray = arrToSparse(arr);
        arrToString(sparseArray);

        //3.保存到本地磁盘
        saveToLocalDish(sparseArray,location);

        //4.从本地磁盘恢复 并且打印出来
        int[][] fromLocalDish = getFromLocalDish(location);
        //因为恢复出来的是一个原数组 所以还需要将其转换成 稀疏数组
        fromLocalDish = arrToSparse(fromLocalDish);
        arrToString(fromLocalDish);

    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值