数组和稀疏数组的相互转换并存储在txt文件中

package com.yg.dataStructure;/*
@author  Ge
@date    2020/2/17  14:45
*/

import java.io.*;

public class SparseArray {
    public static void main(String[] args) {
        int chessArr1[][] = new int[11][11];
        chessArr1[1][2] = 1;
        chessArr1[2][3] = 2;
        int sparseArr[][];
        int sum = getDataCount(chessArr1);
        sparseArr = arrTransferSparseArr(chessArr1, sum);
        //showChessArr(sparseArr);
        //将稀疏数组存入文件中
         putSparseArrInFile(sparseArr);
        //重文件中读取稀疏数组
        File file = new File("D:/ArrData.txt");
        int sparseArr2[][]=getSparseArrInFile(file, sum);;
       // showChessArr(sparseArr2);
        //将稀疏数组变成原数组
        int [][]chessArr2=sparseArrTransferArr(sparseArr2);
        showChessArr(chessArr2);

    }

    private static int [][] sparseArrTransferArr(int[][] sparseArr2) {
        int [][]chessArr2=new int[sparseArr2[0][0]][sparseArr2[0][1]];
        for (int i = 1; i <sparseArr2.length ; i++) {
            chessArr2[sparseArr2[i][0]][sparseArr2[i][1]]=sparseArr2[i][2];
        }
        return chessArr2;
    }

    private static int[][] getSparseArrInFile(File file, int sum) {

            int[][] sparseArr = new int[sum + 1][3];
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String line = "";
            int row=0;
            while ((line = reader.readLine()) != null) {
                String[] tem = line.split("\t");
                for (int i = 0; i < tem.length; i++) {
                    sparseArr[row][i]=Integer.parseInt(tem[i]);
                }
                row++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sparseArr;
    }

    private static void putSparseArrInFile(int[][] sparseArr) {
        File file = new File("D:/ArrData.txt");
        if (file.exists()) {
            file.mkdir();
        }
        try {
            FileWriter writer = new FileWriter(file);
            for (int i = 0; i < sparseArr.length; i++) {
                for (int j = 0; j < sparseArr[0].length; j++) {
                    writer.write(sparseArr[i][j] + "\t");
                }
                writer.write("\n");

            }
            writer.flush();
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    //得到有效数据的个数
    private static int getDataCount(int[][] chessArr1) {
        int sum = 0;
        for (int i = 0; i < chessArr1.length; i++) {
            for (int j = 0; j < chessArr1[0].length; j++) {
                if (chessArr1[i][j] != 0) {
                    sum++;
                }
            }
        }
        return sum;
    }

    //数组转成稀疏数组
    private static int[][] arrTransferSparseArr(int[][] chessArr1, int sum) {
        int count = 0;//用于记录稀疏数组的行数
        int sparseArr[][] = new int[sum + 1][3];
        sparseArr[0][0] = chessArr1.length;
        sparseArr[0][1] = chessArr1[0].length;
        for (int i = 0; i < chessArr1.length; i++) {
            for (int j = 0; j < chessArr1[0].length; j++) {
                if (chessArr1[i][j] != 0) {
                    count++;
                    sparseArr[count][0] = i;
                    sparseArr[count][1] = j;
                    sparseArr[count][2] = chessArr1[i][j];
                }
            }
        }
        return sparseArr;
    }

    //打印棋盘
    public static void showChessArr(int[][] chessArr1) {

        for (int[] arr : chessArr1) {
            for (int dataItem : arr) {
                System.out.print(dataItem + "\t");
            }
            System.out.println();
        }
    }

}


发布了23 篇原创文章 · 获赞 8 · 访问量 1386

猜你喜欢

转载自blog.csdn.net/lin1214000999/article/details/104379026