转换稀疏数组到磁盘并且读出来数组(尚硅谷数据结构笔记)

转换稀疏数组到磁盘并且读出来数组(尚硅谷数据结构笔记)

提示:项目Maven构建 Java实现 idea


一、直接上代码

代码如下(示例):

package com.Array;

import java.io.*;
import java.util.Arrays;

public class SparseArr {
    public static void main(String[] args) {
//        创建一个原始的二维数组11*11
//    0:表示没有棋子,1表示黑子2表蓝子
        int chessArr[][] = new int[11][11];
        chessArr[1][2] = 1;
        chessArr[2][4] = 2;


        //转换稀疏数组
        int[][] sparse = toSparse(chessArr);
        for (int[] inner : sparse) {
            for (int data : inner) {
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }


        saveArray(sparse);

        //还原数组
        System.out.println();
        int[][] array = toArray(sparse);
        for (int[] inner : array) {
            for (int data : inner) {
                System.out.printf("%d\t",data);
            }
            System.out.println();
        }


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


    }

    /**
     *
     * @param sparse 稀疏数组(还原)
     */
    private static int[][] toArray(int[][] sparse) {
        int row = sparse[0][0];
        int column = sparse[0][1];
        //建立好待还原数组
        int[][] array = new int[row][column];
        //稀疏数组值填充待还原数组
        for (int i = 1;i < sparse.length;i++){
            //计算元素在待还原数组的坐标和值
            int x = sparse[i][0];
            int y = sparse[i][1];
            int value = sparse[i][2];
            array[x][y] = value;
        }

        return array;
    }

    /**
     *
     * @param array 普通数组转换稀疏数组
     * @return
     */
    public static int[][] toSparse(int[][] array){
        //计算数组中不为0的个数
        int sum = 0;
        for (int[] inner : array) {
            for (int i = 0;i < inner.length;i++){
                if (inner[i] != 0){
                    sum ++;
                }
            }
        }

        //创建稀疏数组
        int[][] sparseArray = new int[sum + 1][3];

        //初始化第一行
        sparseArray[0][0] = array[array.length-1].length;
        sparseArray[0][1] = array.length;
        sparseArray[0][2] = sum;

        //把数据存入稀疏数组
        //count用来计数
        int count = 0;
        for (int i = 0; i < array.length;i++){
            for (int j = 0; j < array[i].length; j++){
                if (array[i][j] != 0){
                    ++count;
                    sparseArray[count][0] = i;
                    sparseArray[count][1] = j;
                    sparseArray[count][2] = array[i][j];
                }
            }
        }

        return sparseArray;
    }
    //保存数组
    public static boolean saveArray(int[][] array){
        File file  = null;
        FileWriter writer = null;
        try {
            //我是Mac系统路径,注意要是win系统可能修改路径符号
            file = new File("./src/main/resources/map.txt");
            if (!file.exists()){
                file.createNewFile();
            }
             writer = new FileWriter(file,true);
            for (int[] inner : array) {
                for (int data : inner) {
                    writer.write(    data + "\t");
                }
                writer.write("\n");
            }
            return true;
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }finally {
            if (writer != null){
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }


    //加载数组到内存中
    public static int[][] loadArray(){
        File file = null;
        FileReader reader = null;
        //先把文件里的数据存buffer里
        StringBuffer buffer = new StringBuffer();
        try {
            file = new File("./src/main/resources/map.txt");
            reader = new FileReader(file);
            int len = 0;
            char[] data = new char[10];
            while ((len = reader.read(data)) != -1){
                for (int i = 0; i < len;i++){
                    buffer.append(data[i]);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if (reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        //将buffer转换string
        String result = new String(buffer);
        //正则表达式拆分字符串(拆每行)
        String[] row = result.split("\n");
        //拆0行有多少列 为new
        String[] col = row[0].split("\t");
        //准备填充数组准备 返回returnArray确定列树木
        int[][] returnArray = new int[row.length][col.length];
        for (int i = 0;i < row.length;i++){
            //拆分每行
            String[] column = row[i].split("\t");
            for (int j = 0;j < column.length;j++){
                //将string转换int类型
                returnArray[i][j] = Integer.parseInt(column[j]);
            }
        }
        return returnArray;
    }




}


总结

提示:关于IO读写你可以加缓冲流加快读写速度:代码参考尚硅谷老师代码完成的

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值