zoukankan      html  css  js  c++  java
  • 稀疏数组的实现

    原理
    对于一些比较大的二维数组,其中有很多的位置为零,也就是无效的数据,我们在保存的时候只需要保存数组有数据的位置即可,这就是稀疏数组。
    稀疏数组的第一行第0列是二维矩阵的行数,第1列是二维矩阵的列数,第2列是二维矩阵中的有效数据个数
    在后几行的第0列是有效数据的行坐标,第1列是列坐标,第2列是有效数据的数值。
    之后我们将稀疏数组保存到文件中,这样就比直接保存二维数组更加节省空间。
    从文件中读取出稀疏数组,先读取第一行,创建出二维矩阵,之后遍历将有效数据存入二维数组,实现了从稀疏数组恢复为二维数组。

    package sparseArray;
    
    import java.io.*;
    import java.util.Arrays;
    
    /**
     * 稀疏数组
     */
    public class SparseArray {
        public static void main(String[] args) throws IOException {
            //创建一个11*11的二维数组
            //0表示没有棋子,1表示黑子,2表示白子
            int chessArray01[][] = new int[11][11];
            chessArray01[1][2] = 1;
            chessArray01[2][3] = 2;
            chessArray01[5][6] = 1;
            //原始的二维数组
            System.out.println("原始的二维数组为:");
            for (int[] row:chessArray01){
                for (int data :row){
                    System.out.printf("%d	",data);
                }
                System.out.println();
            }
            //将二维数组转为稀疏数组的思路
    
            //先遍历二维数组,得到非零数据的个数
            int sum = 0;
            for (int i = 0; i < chessArray01.length; i++) {
                for (int j = 0; j < chessArray01[0].length; j++) {
                    if (chessArray01[i][j]!=0){
                        sum++;
                    }
                }
            }
            //创建稀疏数组
            int sparseArray[][] = new int[sum+1][3];
            //给稀疏数组赋值
            sparseArray[0][0] = 11;
            sparseArray[0][1] = 11;
            sparseArray[0][2] = sum;
            //遍历二维数组,将非零的值存放到稀疏数组
            int count = 0;//count用来记录是第几个非零数据
            for (int i = 0; i < chessArray01.length; i++) {
                for (int j = 0; j < chessArray01[0].length; j++) {
                    if (chessArray01[i][j]!=0){
                        count++;//记录一次非零数据
                        sparseArray[count][0] = i;
                        sparseArray[count][1] = j;
                        sparseArray[count][2] = chessArray01[i][j];
                    }
                }
            }
            //输出稀疏数组
            System.out.println("得到的稀疏数组为:");
            for (int i = 0; i <sparseArray.length ; i++) {
                System.out.printf("%d	%d	%d	
    ",sparseArray[i][0],sparseArray[i][1],sparseArray[i][2]);
            }
            //将得到的稀疏数组保存到文件map.data中
            File file = new File("map.data");
            FileWriter out = new FileWriter(file);
            for (int i = 0; i <sparseArray.length ; i++) {
                out.write(sparseArray[i][0]+"	"+sparseArray[i][1]+"	"+sparseArray[i][2]+"
    ");
            }
            out.close();
            //从文件中读取稀疏数组
            BufferedReader in = new BufferedReader(new FileReader(file));
            //定义新的稀疏数组
            String line;//一行数据
            int rowCount = 0;
            int [][] sparseArray02 = {};
            //读取第一行数据
            while ((line = in.readLine())!=null){
                if (rowCount==0){
                    String[] str = line.split("	");
                    sparseArray02 = new int[Integer.parseInt(str[2])+1][3];
                    sparseArray02[rowCount][0] = Integer.parseInt(str[0]);
                    sparseArray02[rowCount][1] = Integer.parseInt(str[1]);
                    sparseArray02[rowCount][2] = Integer.parseInt(str[2]);
                    rowCount++;
                }else {
                    String[] str = line.split("	");
                    sparseArray02[rowCount][0] = Integer.parseInt(str[0]);
                    sparseArray02[rowCount][1] = Integer.parseInt(str[1]);
                    sparseArray02[rowCount][2] = Integer.parseInt(str[2]);
                    rowCount++;
                }
            };
            //输出从文件中读取的稀疏数组
            System.out.println("从文件中读取的稀疏数组为:");
            for (int i = 0; i <sparseArray02.length ; i++) {
                System.out.printf("%d	%d	%d	
    ",sparseArray02[i][0],sparseArray02[i][1],sparseArray02[i][2]);
            }
    
            //将稀疏数组恢复成二维数组
            //根据稀疏数组的第一行创建二维数组
            int chessArray02[][] = new int[sparseArray02[0][0]][sparseArray02[0][1]];
            //循环稀疏数组的后几行,给二维数组赋值
            for (int i = 1; i <sparseArray02.length ; i++) {
                chessArray02[sparseArray02[i][0]][sparseArray02[i][1]] = sparseArray02[i][2];
            }
            //输出恢复后的二维数组
            System.out.println("恢复后的二维数组为:");
            for (int[] row:chessArray02){
                for (int data :row){
                    System.out.printf("%d	",data);
                }
                System.out.println();
            }
    
        }
    }
    
    

  • 相关阅读:
    极光推送
    ModelAndView跳转页面的时候,显示了页面的源码问题
    关于字符串比较时候出现的空指针问题的坑
    JAVA的extends用法
    C# 平面文件批量导数据到DB(二)
    C# 平面文件批量导数据到DB(一)
    C#操作文件和文件夹的类介绍
    C# 实现监控文件夹和里面文件的变化
    SQL Server 2012 Throw关键字
    SQL SERVER EXCEPT 和 INTERSECT
  • 原文地址:https://www.cnblogs.com/yeyueweiliang/p/14641873.html
Copyright © 2011-2022 走看看