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
    评论
实现虚拟磁盘转储功能,需要以下几个步骤: 1. 定义磁盘块结构体 首先需要定义一个磁盘块结构体,用于表示磁盘的一个块,包括块号、块大小和块数据等信息。例如: ``` typedef struct { int block_num; // 块号 int block_size; // 块大小 char data[BLOCK_SIZE]; // 块数据 } Block; ``` 2. 定义虚拟磁盘结构体 定义一个虚拟磁盘结构体,用于表示整个磁盘包括磁盘大小、块大小、磁盘数组等信息。例如: ``` typedef struct { int disk_size; // 磁盘大小 int block_size; // 块大小 Block* blocks; // 磁盘数组 } Disk; ``` 3. 初始化虚拟磁盘 在程序开始时,需要初始化虚拟磁盘包括分配磁盘数组和设置每个磁盘块的块号和大小等信息。例如: ``` Disk* disk = (Disk*)malloc(sizeof(Disk)); disk->disk_size = DISK_SIZE; disk->block_size = BLOCK_SIZE; disk->blocks = (Block*)malloc(sizeof(Block) * BLOCK_NUM); for (int i = 0; i < BLOCK_NUM; i++) { disk->blocks[i].block_num = i; disk->blocks[i].block_size = BLOCK_SIZE; } ``` 4. 写入磁盘 当需要将信息存入磁盘时,先需要将信息分割成多个块,然后将每个块写入虚拟磁盘中对应的磁盘块。例如: ``` void write_to_disk(Disk* disk, int block_num, char* data, int size) { int block_count = (size + BLOCK_SIZE - 1) / BLOCK_SIZE; // 计算需要写入的块数 for (int i = 0; i < block_count; i++) { int pos = block_num + i; memcpy(disk->blocks[pos].data, data + i * BLOCK_SIZE, BLOCK_SIZE); } } ``` 5. 从磁盘读入内存 当需要从磁盘读取信息时,先需要找到对应的磁盘块,然后将每个磁盘块中的数据读取出来拼接成完整的信息。例如: ``` char* read_from_disk(Disk* disk, int block_num, int size) { char* data = (char*)malloc(size); int block_count = (size + BLOCK_SIZE - 1) / BLOCK_SIZE; // 计算需要读取的块数 for (int i = 0; i < block_count; i++) { int pos = block_num + i; memcpy(data + i * BLOCK_SIZE, disk->blocks[pos].data, BLOCK_SIZE); } return data; } ``` 以上就是实现虚拟磁盘转储功能的主要步骤,可以根据需要进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值