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


    二维数组转稀疏数组

    思路:
    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;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

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

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

        //从文件读入数组
        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
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39

    从稀疏数组转二维数组

    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;
        }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

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

        //将数据写入文件中
        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();
                }
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    完整代码

    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();
                }
            }
    
        }
    
    }
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180

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

  • 相关阅读:
    SpringBoot SpringBoot 运维实用篇 1 打包与运行 1.2 打包插件
    数字货币风暴:比特币价格突破历史新高,引发金融市场震荡
    首个落地实践!曙光存储案例入选液冷数据中心白皮书
    openGauss内核分析-统计信息与行数估计
    React18组件一键转换Vue3组件
    ElasticSearch的安装部署-----图文介绍
    信息学奥赛一本通 1353:表达式括号匹配(stack) | 洛谷 P1739 表达式括号匹配
    select语句查询数据 并新增字段列并赋值
    【Redis】共同关注列表与基于Feed流的关注消息滚动分页推送的实现
    [附源码]Python计算机毕业设计Django考试系统
  • 原文地址:https://blog.csdn.net/ou_yang_mu_ling/article/details/125623554