Convert sparse array to disk and read out the array (Silicon Valley Data Structure Notes)

Convert sparse array to disk and read out the array (Silicon Valley Data Structure Notes)

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


1. Directly upload the code

The code is as follows (example):

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;
    }




}


Summarize

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

Guess you like

Origin blog.csdn.net/prjh_/article/details/124656675