java实现稀疏数组(包括存入磁盘、从磁盘读取)

原二维数组
在这里插入图片描述
稀疏数组
在这里插入图片描述

package ShangGuiGu.sparseArray;

import java.io.*;

public class SparseArrayTest {

    /**
     * 将稀疏数组存盘
     * @param sparseArray
     */
    public static void saveToFile(int[][] sparseArray){
        FileWriter fileWriter=null;
        try {
            fileWriter= new FileWriter(new File("D:\\sparseArray.data"));
            for (int[] array : sparseArray) {
                fileWriter.write(array[0]+"\t"+array[1]+"\t"+array[2]);
                fileWriter.write("\r\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                fileWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 从磁盘中读取稀疏数组
     * @return
     */
    public static int[][] readFromFile(){
        int[][] sparseArray2=null;
        boolean isNotRead=false;
        BufferedReader bufferedReader=null;
        try {
            bufferedReader=new BufferedReader(new FileReader(new File("D:\\sparseArray.data")));
            String lineStr=null;
            int curCount=0;
            while ((lineStr=bufferedReader.readLine())!=null){
                String[] tempStr=lineStr.split("\t");
                if(!isNotRead){
                    sparseArray2=new int[Integer.parseInt(tempStr[2])+1][3];
                    sparseArray2[curCount][0]=Integer.parseInt(tempStr[0]);
                    sparseArray2[curCount][1]=Integer.parseInt(tempStr[1]);
                    sparseArray2[curCount][2]=Integer.parseInt(tempStr[2]);
                    curCount++;
                    isNotRead=true;
                }else {
                    sparseArray2[curCount][0]=Integer.parseInt(tempStr[0]);
                    sparseArray2[curCount][1]=Integer.parseInt(tempStr[1]);
                    sparseArray2[curCount][2]=Integer.parseInt(tempStr[2]);
                    curCount++;
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bufferedReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sparseArray2;
    }

    public static void main(String[] args) {
        int row=8,column=8;
        int[][] sourceArray=new int[row][column];
        sourceArray[1][2]=5;
        sourceArray[1][3]=4;
        sourceArray[2][5]=1;
        sourceArray[2][7]=4;
        sourceArray[7][1]=9;
        //遍历原二维数组
        System.out.println("----------原二维数组如下-----------");
        int count=0;
        for (int[] array : sourceArray) {
            for (int i = 0; i <array.length ; i++) {
                System.out.printf("%d  ",array[i]);
                if (array[i]>0){
                    count++;
                }
            }
            System.out.println();
        }
        int[][] sparseArray=new int[count+1][3];
        sparseArray[0][0]=row;
        sparseArray[0][1]=column;
        sparseArray[0][2]=count;
        //转为稀疏数组
        System.out.println("------------稀疏数组如下------------");
        int cur=0;
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < column; j++) {
                if(sourceArray[i][j]>0){
                    cur++;
                    sparseArray[cur][0]=i;
                    sparseArray[cur][1]=j;
                    sparseArray[cur][2]=sourceArray[i][j];
                }
            }
        }
        //遍历稀疏数组
        for (int[] array : sparseArray) {
            System.out.printf("%d %d %d\n",array[0],array[1],array[2]);
        }

        /**
        * 将稀疏数组存盘
        */
        saveToFile(sparseArray);
        /**
         *从磁盘中读取稀疏数组
         */
        int[][] sparseArray2=readFromFile();

        //转化为二维数组
        int row2=sparseArray2[0][0];
        int column2=sparseArray2[0][1];
        int[][] sourceArray2=new int[row2][column2];
        for (int i = 1; i < sparseArray2.length; i++) {
            sourceArray2[sparseArray2[i][0]][sparseArray2[i][1]]=sparseArray2[i][2];
        }
        //遍历二维数组
        System.out.println("-----------原二维数组如下-----------");
        for (int[] array : sourceArray2) {
            for (int i = 0; i <array.length ; i++) {
                System.out.printf("%d  ",array[i]);
                if (array[i]>0){
                    count++;
                }
            }
            System.out.println();
        }



    }
}

运行结果如下
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值