稀疏数组(Java)

本文介绍了稀疏数组的概念,当数组中大部分元素为0时,使用稀疏数组可以有效节省空间。作者提供了Java代码实现,展示了如何创建、显示、写入和读取稀疏数组,并通过实例验证了其正确性。代码包括将二维数组转换为稀疏数组,以及从文件中读取和恢复稀疏数组的过程。
摘要由CSDN通过智能技术生成

稀疏数组基本介绍

当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组

  • 记录数组一共有几行几列,有多少个不同的值
  • 把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模

图解
在这里插入图片描述

代码实现

package com.atschool.sparsearr;

import java.io.*;

/**
 * Description:
 * Author:江洋大盗
 * Date:2020/11/28 22:01
 */
public class SparseArr {
    public static void main(String[] args) {
        SparseArr sparseArr = new SparseArr();
        int[][] arr01 = new int[11][11];
        arr01[1][2] = 1;
        arr01[2][3] = 2;
        sparseArr.showArr(arr01);

        int sum = 0;
        for (int[] ints : arr01) {
            for (int anInt : ints) {
                if (anInt != 0) {
                    sum++;
                }
            }
        }

        int[][] arr02 = new int[sum + 1][3];
        arr02[0][0] = 11;
        arr02[0][1] = 11;
        arr02[0][2] = sum;
        int count = 0;
        for (int i = 0; i < arr01.length; i++) {
            for (int j = 0; j < arr01[i].length; j++) {
                if (arr01[i][j] != 0) {
                    count++;
                    arr02[count][0] = i;
                    arr02[count][1] = j;
                    arr02[count][2] = arr01[i][j];
                }
            }
        }

        sparseArr.showArr(arr02);
        sparseArr.write(arr02);
        int[][] arr03 = (int[][]) sparseArr.read(new File("E:\\ioTest\\sparseArr.dat"));
        System.out.println("-------------------------");
        assert arr03 != null;
        sparseArr.showArr(arr03);

        int[][] arr04 = new int[arr03[0][0]][arr03[0][1]];

        for (int i = 1; i < arr03.length; i++) {
            arr04[arr03[i][0]][arr03[i][1]] = arr03[i][2];
        }

        System.out.println("----------------------");
        sparseArr.showArr(arr04);

    }

    private void showArr(int[][] arr) {
        for (int[] ints : arr) {
            for (int anInt : ints) {
                System.out.print(anInt + "\t");
            }
            System.out.println();
        }
    }

    private void write(Object obj) {
        ObjectOutputStream oos = null;
        try {
            FileOutputStream fos = new FileOutputStream(new File("E:\\ioTest\\sparseArr.dat"));
            oos = new ObjectOutputStream(fos);
            oos.writeObject(obj);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    private Object read(File file) {
        ObjectInputStream ois = null;
        try {
            FileInputStream fis = new FileInputStream(file);
            ois = new ObjectInputStream(fis);
            return ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ois != null) {
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
        return null;
    }
}

结语

只要能收获甜蜜,荆棘丛中也有蜜蜂忙碌的身影,未来的你一定会感谢现在努力的自己。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值