一溪风月
一溪风月
Published on 2023-04-10 / 61 Visits
0

稀疏数组(Java)

稀疏数组

稀疏数组基本介绍

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

  • 记录数组一共有几行几列,有多少个不同的值

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

图解


Snipaste_2023-04-10_17-52-54.png

代码实现

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;
    }
}

结语

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