1.稀疏数组

稀疏数组

基本功能

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

处理方法

记录数组一共有几行几列,有多少个不同的值
把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模
在这里插入图片描述

转换思路

二维数组转稀疏数组

遍历二维数组,得到二维数组中有效值的个数sum
创建稀疏数组,有sum+1行,3列(固定)即sparseArr int[sum + 1] [3]
将二维数组中的有效值存入稀疏数组中

稀疏数组转二维数组

先读取稀疏数组的第一行(保存二维数组的行列信息),还原二维数组
读取稀疏数组的其他行,将值赋给二维数组的对应位置上的数

rowcolval
0几行几列有效数据总数
1
2

代码

数组转换为稀疏数组存储到磁盘,读取出之后转化显示。

代码示例

码云地址https://gitee.com/magneto/codeReview/tree/master/SGGDataStructure/DataStructure

package top.pro51.sparsearray;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @ClassName SparseArray
 * @Description: 稀疏数组
 * @Author: WangWenpeng
 * @date: 9:52 2020/6/1
 * @Version 1.0
 */
public class SparseArray {
    public static void main(String[] args) throws IOException {

        //初始二维数组的有效数据3个   其他默认是0  初始化棋盘大小11
        String fileName = "C:\\IdeaCode\\CodeReview\\codeReview\\SGGDataStructure\\DataFile\\sparseArray.txt";
        int chessDefault = 11;
        int[][] chessArray = new int[chessDefault][chessDefault];
        chessArray[1][2] = 1;
        chessArray[2][3] = 2;
        chessArray[4][5] = 2;

        //输出查看原标准二维数组
        System.out.println("1.原始数组输出----:");
        printArray(chessArray);

        //转化为稀疏数组并输出查看
        System.out.println("2.转换为稀疏数组输出----:");
        int[][] sparseArray = toSparseArray(chessArray, chessDefault);
        printArray(sparseArray);

        //存储二维数组到磁盘
        System.out.println("3.存数稀疏数组到磁盘----:");
        writeArraytoDisk(fileName, sparseArray);

        System.out.println("4.读取稀疏数组到内存----:");
        int[][] readArray = readArrayFromdisk(fileName);
        printArray(readArray);

        System.out.println("5.稀疏数组还原初始二维数组----:");
        int[][] sourceArray = new int[readArray[0][0]][readArray[0][1]];
        for (int i = 1; i < readArray.length; i++) {
            sourceArray[readArray[i][0]][readArray[i][1]] = readArray[i][2];
        }

        System.out.println("6.原始数组输出----:");
        printArray(sourceArray);
    }

    /**
     * @Description 读取稀疏数组文件
     * @Author WangWenpeng
     * @Date 14:15 2020/6/1
     * @Param [fileName]
     */
    private static int[][] readArrayFromdisk(String fileName) throws IOException {
        //未知文件 几行几列
        int row = 0, col = 0;
        List<int[]> readList = new ArrayList<>();

        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(fileName))));
        String lineTxt = null;
        while ((lineTxt = bufferedReader.readLine()) != null) {
            //读取的String 转换为int数组存数在ArrayList中
            int[] array = Arrays.stream(lineTxt.split("\\t")).mapToInt(Integer::parseInt).toArray();
            System.out.println("读取稀疏数组----:" + Arrays.toString(array));
            readList.add(array);
            col = array.length;
        }
        row = readList.size();
        bufferedReader.close();

        // ArrayList内部一维数组转化为二维数组
        int[][] readArray = new int[row][col];
        for (int i = 0; i < readArray.length; i++) {
            readArray[i] = readList.get(i);
        }
        return readArray;
    }

    /**
     * @Description 存储数组到磁盘
     * @Author WangWenpeng
     * @Date 11:50 2020/6/1
     * @Param [fileName, sparseArray]
     */
    private static void writeArraytoDisk(String fileName, int[][] sparseArray) throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File(fileName), false));
        StringBuffer sb = new StringBuffer();
        for (int[] row : sparseArray) {
            for (int i : row) {
                sb.append(i + "\t");
            }
            sb.append("\r\n");
        }
        bw.write(sb.toString());
        bw.flush();
        bw.close();
    }

    /**
     * @Description 转换稀疏数组方法
     * @Author WangWenpeng
     * @Date 11:25 2020/6/1
     * @Param [chessArray]
     */
    private static int[][] toSparseArray(int[][] chessArray, int chessDefault) {
        //转化为3列稀疏数组  有效数据sum++
        int sum = 0;
        for (int i = 0; i < chessArray.length; i++) {
            for (int j = 0; j < chessArray[i].length; j++) {
                if (chessArray[i][j] != 0) {
                    sum++;
                }
            }
        }

        //创建稀疏数组 3列 有效数据+1行  第0行为数组基本信息
        int[][] sparseArray = new int[sum + 1][3];
        sparseArray[0][0] = chessDefault;
        sparseArray[0][1] = chessDefault;
        sparseArray[0][2] = sum;

        //第1行开始放数据
        int count = 1;
        for (int i = 0; i < chessArray.length; i++) {
            for (int j = 0; j < chessArray[i].length; j++) {
                if (chessArray[i][j] != 0) {
                    sparseArray[count][0] = i;
                    sparseArray[count][1] = j;
                    sparseArray[count][2] = chessArray[i][j];
                    count++;
                }
            }
        }
        return sparseArray;
    }

    /**
     * @Description 二维数组标准输出
     * @Author WangWenpeng
     * @Date 9:59 2020/6/1
     * @Param [twoDimensionalArray]
     */
    public static void printArray(int[][] twoDimensionalArray) {
        System.out.println("二维数组标准输出---begin");
        for (int[] row : twoDimensionalArray) {
            for (int i : row) {
                System.out.printf("%d\t", i);
            }
            System.out.println();
        }
        System.out.println("二维数组标准输出---end");
    }

}

微信
微信

个人网站
http://www.51pro.top
网站
公众号
公众号

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值