二维数组与稀疏数组的互转实现与写入写出

二维数组转稀疏数组

思路:
1、遍历原始数组,得到有效数据个数count;
2、根据第一步得到的数据新建稀疏数组arry[count][3];
3、将有效数据存入稀疏数组

    //二维数组转稀疏数组
    public static int[][] TwoDimensionalToSparseMartix(int[][] TwoDimensionalArray) {

        int[][] TDA = TwoDimensionalArray;
        int count = 0;
        //循环内遍历得到二维数组内的有效值个数
        //用来新建数组时候,确定数组行数
        for (int i = 0; i < TDA.length; i++) {
            for (int j = 0; j < TDA[0].length; j++) {
                if (TDA[i][j] != 0) {
                    count++;
                }
            }
        }

        int[][] SAM = new int[count + 1][3];
        SAM[0][0] = TDA.length;
        SAM[0][1] = TDA[0].length;
        SAM[0][2] = count;

        int flag = 0;
        //循环赋值
        for (int i = 0; i < TDA.length; i++) {
            for (int j = 0; j < TDA[0].length; j++) {
                if (TDA[i][j] != 0) {
                    SAM[++flag][0] = i;
                    SAM[flag][1] = j;
                    SAM[flag][2] = TDA[i][j];
                }
            }
        }
        return SAM;
    }

从指定路径下的文件中读取到数据

利用输入输出流来做(此处有借鉴其他视频及博文)

    //从文件读入数组
    public static int[][] readFromFile(String Path) throws IOException {
        File file = new File(Path);
        int[][] SMA = null;
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(file));
            //获取稀疏数组大小
            int sum = 0;
            while (bufferedReader.readLine() != null) {
                sum++;
            }
            SMA = new int[sum][3];
            //读完以后关闭流,防止后面读到的为空
            bufferedReader.close();
            //打开流
            bufferedReader = new BufferedReader(new FileReader(file));

            String strs = bufferedReader.readLine();
            int i = 0;
            int j = 0;
            while (strs != null) {
                String[] str = strs.split(" ");
                for(String s :str){
                    SMA[i][j] = Integer.parseInt(s);
                    j++;
                }
                j = 0;
                i++;//行指针自增
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        if(bufferedReader != null) bufferedReader.close();
        return SMA;
    }

从稀疏数组转二维数组

1、通过稀疏数组的第一行,新建二维数组。
2、遍历稀疏数组,根据第2-n行数据将有效值赋值给二维数组

    //稀疏数组转二维数组
    public static int[][] SparseMartixToTwoDimensional(int[][] SparseMartixArray) {
        int[][] SAM = SparseMartixArray;
        //根据稀疏数组的第一行初始化得到二维数组
        int[][] TDA = new int[SAM[0][0]][SAM[0][1]];

        //根据稀疏矩阵数据还原
        for (int i = 1; i < SAM.length; i++) {
            TDA[SAM[i][0]][SAM[i][1]] = SAM[i][2];
        }
        return TDA;
    }

将数组输出到指定路径文件

    //将数据写入文件中
    public static void writeTotext(int[][] array, String path) {
        File file = new File(path);
        //创建字符输出流
        FileWriter writer = null;
        try {
            writer = new FileWriter(file, true);
            //写数据
            for (int i = 0; i < array.length; i++) {
                writer.write(array[i][0] + " ");
                writer.write(array[i][1] + " ");
                writer.write(array[i][2] + " " + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //关闭流
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

完整代码

package SparseMatrix;

import java.io.*;
import java.util.Scanner;

/**
 * @author fuyaling
 * @create 2022-06-28-17:07
 * 稀疏矩阵的代码实现
 */
public class SparseMartrix {
    public static void main(String[] args) {
        //新建scanner对象接收输入
        Scanner scanner = new Scanner(System.in);
        //获取数组行数
        int m = scanner.nextInt();
        //获取数组列数
        int n = scanner.nextInt();
        //初始化m行n列的数组
        int[][] arry = InitialValue(m, n);
        //输出原数组
        PrintArry(arry);
        //转稀疏数组
        int[][] SparseMartix = TwoDimensionalToSparseMartix(arry);
        //输出转后的系数数组
        PrintArry(SparseMartix);

        //稀疏数组转二维数组
        int[][] TDA = SparseMartixToTwoDimensional(SparseMartix);
        PrintArry(TDA);

    }

    //遍历二维数组
    public static void PrintArry(int[][] arry) {
        if (arry != null) {
            for (int i = 0; i < arry.length; i++) {
                for (int j = 0; j < arry[i].length; j++) {
                    System.out.print(arry[i][j]);
                }
                System.out.println();
            }

        }

    }

    //获取原数组并赋值
    public static int[][] InitialValue(int m, int n) {
        //新建m行n列的二维数组
        int[][] arry = new int[m][n];
        for (int i = 0; i < m; i++) {
            for (int j = 0; j < n; j++) {
                if (j == i - 1) {
                    arry[i][j] = i;
                }//else arry[i][j] = 0;
            }
        }
        return arry;
    }

    //二维数组转稀疏数组
    public static int[][] TwoDimensionalToSparseMartix(int[][] TwoDimensionalArray) {

        int[][] TDA = TwoDimensionalArray;
        int count = 0;
        //循环内遍历得到二维数组内的有效值个数
        //用来新建数组时候,确定数组行数
        for (int i = 0; i < TDA.length; i++) {
            for (int j = 0; j < TDA[0].length; j++) {
                if (TDA[i][j] != 0) {
                    count++;
                }
            }
        }

        int[][] SAM = new int[count + 1][3];
        SAM[0][0] = TDA.length;
        SAM[0][1] = TDA[0].length;
        SAM[0][2] = count;

        int flag = 0;
        //循环赋值
        for (int i = 0; i < TDA.length; i++) {
            for (int j = 0; j < TDA[0].length; j++) {
                if (TDA[i][j] != 0) {
                    SAM[++flag][0] = i;
                    SAM[flag][1] = j;
                    SAM[flag][2] = TDA[i][j];
                }
            }
        }
        return SAM;
    }


    //稀疏数组转二维数组
    public static int[][] SparseMartixToTwoDimensional(int[][] SparseMartixArray) {
        int[][] SAM = SparseMartixArray;
        //根据稀疏数组的第一行初始化得到二维数组
        int[][] TDA = new int[SAM[0][0]][SAM[0][1]];

        //根据稀疏矩阵数据还原
        for (int i = 1; i < SAM.length; i++) {
            TDA[SAM[i][0]][SAM[i][1]] = SAM[i][2];
        }
        return TDA;
    }

    //从文件读入数组
    public static int[][] readFromFile(String Path) throws IOException {
        File file = new File(Path);
        int[][] SMA = null;
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(file));
            //获取稀疏数组大小
            int sum = 0;
            while (bufferedReader.readLine() != null) {
                sum++;
            }
            SMA = new int[sum][3];
            //读完以后关闭流,防止后面读到的为空
            bufferedReader.close();
            //打开流
            bufferedReader = new BufferedReader(new FileReader(file));

            String strs = bufferedReader.readLine();
            int i = 0;
            int j = 0;
            while (strs != null) {
                String[] str = strs.split(" ");
                for (String s : str) {
                    SMA[i][j] = Integer.parseInt(s);
                    j++;
                }
                j = 0;
                i++;//行指针自增
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }

        if (bufferedReader != null) bufferedReader.close();
        return SMA;
    }


    //将数据写入文件中
    public static void writeTotext(int[][] array, String path) {
        File file = new File(path);
        //创建字符输出流
        FileWriter writer = null;
        try {
            writer = new FileWriter(file, true);
            //写数据
            for (int i = 0; i < array.length; i++) {
                writer.write(array[i][0] + " ");
                writer.write(array[i][1] + " ");
                writer.write(array[i][2] + " " + "\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        //关闭流
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}


编码养成良好习惯,特别是Java学习,对于复用性高,容易造成不必要冗余的且能封装的方法尽量封装。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值