二维数组转稀疏数组再存入磁盘并方向操作

package com.xch.sparsearray;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class SparseArrayReplay {
    public static void main(String[] args) {
        // 搞个二维数组
        int[][] chessArr1 = generateTheSpecifiedTwoDimensionalArray(11, 11);
        // 加些非0数字
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        chessArr1[4][5] = 1;
        // 二维数组转化为稀疏数组
        int[][] sparseArr1 = convertTwoDimensionalArrayToSparseArray(chessArr1);
        System.out.println("稀疏数组");
        for (int[] ints : sparseArr1) {
            for (int anInt : ints) {
                System.out.printf("%d\t", anInt);
            }
            System.out.println();
        }
        System.out.println();

        // 将稀疏数组存入磁盘中
//        System.out.println(saveSparseArrayToDisk(sparseArr1));

        // 将稀疏数组从磁盘中拿出来
        int[][] sparseArr2 = takeSparseArrayFromDisk();
        System.out.println("从磁盘拿出来后的稀疏数组");
        for (int[] ints : sparseArr2) {
            for (int anInt : ints) {
                System.out.printf("%d\t", anInt);
            }
            System.out.println();
        }
        System.out.println();

        // 将稀疏数组转换为二维数组
        System.out.println("将稀疏数组转换为二维数组");
        int[][] chessArr2 = convertSparseArrayToTwoDimensionalArray(sparseArr2);
        for (int[] ints : chessArr2) {
            for (int anInt : ints) {
                System.out.printf("%d\t", anInt);
            }
            System.out.println();
        }
        System.out.println();
    }

    // 生成指定的二维数组
    public static int[][] generateTheSpecifiedTwoDimensionalArray(int num1, int num2) {
        int[][] arr = new int[num1][num2];
        return arr;
    }

    // 将二维数组转化为稀疏数组
    public static int[][] convertTwoDimensionalArrayToSparseArray(int[][] arr) {
        int rows = arr.length; // 获取二维数组的行数
        int cols = arr[0].length; // 获取二维数组的列数
        int nonZeroDigit = 0; // 非零数字
        for (int[] ints : arr) {
            for (int anInt : ints) {
                if (anInt != 0) {
                    nonZeroDigit++;
                }
            }
        }
        int[][] sparseArr = new int[nonZeroDigit + 1][3]; // 用二维数组的有效数字定义稀疏数组的行, 列数为3(固定)
        sparseArr[0][0] = rows; //二维数组的行
        sparseArr[0][1] = cols; // 列
        sparseArr[0][2] = nonZeroDigit; // 有效数字
        int index = 1;
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < cols; j++) {
                if (arr[i][j] != 0) {
                    sparseArr[index][0] = i;
                    sparseArr[index][1] = j;
                    sparseArr[index][2] = arr[i][j];
                    index++;
                }
            }
        }
        return sparseArr;
    }

    // 将稀疏数组存入磁盘中
    public static Boolean saveSparseArrayToDisk(int[][] arr) {
        File storagePath = new File("D:\\code\\dataStructure\\dataStructures\\sparseArrayReplay");
        try {
            BufferedWriter bw = new BufferedWriter(new FileWriter(storagePath));
            for (int[] ints : arr) {
                for (int anInt : ints) {
                    bw.write(anInt + "\t");
                }
                bw.write("\n");
                bw.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    // 将稀疏数组从磁盘中拿出来
    public static int[][] takeSparseArrayFromDisk() {
        File src = new File("D:\\code\\dataStructure\\dataStructures\\sparseArrayReplay");
        BufferedReader br = null;
        List<Integer> list = new ArrayList<>();
        try {
            br = new BufferedReader(new FileReader(src));
            String str;
            while ((str = br.readLine()) != null) {
                String[] split = str.split("\t");
                for (int i = 0; i < split.length; i++) {
                    list.add(Integer.parseInt(split[i]));
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        int sparseArr[][] = new int[list.get(2) + 1][3];
        int index = 0;
        for (int i = 0; i < list.size(); i += 3) {
            sparseArr[index][0] = list.get(i);
            sparseArr[index][1] = list.get(i + 1);
            sparseArr[index][2] = list.get(i + 2);
            index++;
        }
        return sparseArr;
    }

    // 将稀疏数组转换为二维数组
    public static int[][] convertSparseArrayToTwoDimensionalArray(int[][] arr) {
        int chessArr[][] = new int[arr[0][0]][arr[0][1]];
        for (int i = 1; i < arr.length; i++) {
            chessArr[arr[i][0]][arr[i][1]] = arr[i][2];
        }
        return chessArr;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值