此文章仅作为自己学习过程中的记录和总结,同时会有意地去用英文来做笔记,一些术语的英译不太准确,内容如有错漏也请多指教,谢谢!


一、概述

需要强调的是,稀松数组适用于二维数组中大部分数据具有相同的值的情况,此时稀松数组可以显著地提高算法效率。反之,若不在上述情况下使用稀松数组,则可能得到效率更低下的结果。

稀松数组的结构大体如下:

Index Row Column Value
0 原数组行数 原数组列数 特殊数据的个数
1 特殊数据1所在行数 特殊数据1所在列数 特殊数据1的值
2

此例子为通过稀松数组记录棋盘上的每个格子的当前子色。(未下子:0; 黑子:1;白子:2)。此例子还包含IO相关知识。

步骤

  1. 创建棋盘数组chessArr,并且对其进行初始化。
  2. 根据chessArr创建稀松数组sparseArr并打印。
  3. 将sparseArr存储到文件map.data中。
  4. 从map.data读取数据并根据之创建新的稀松数组。
  5. 利用新创建的稀松数组,对其进行转化,得到新的棋盘数组并打印。

二、代码实现

  • Attributes and main
/*SparseArrayZzay2021/01/14*/
package com.zzay.sparsearray;import java.io.*;/*** CONTENTS:* (1) Initialize the chess array.* (2) Create a sparse array according to the chess array.* (3) Save the sparse array into the dest file "map.data".* (4) Read the sparse array from the source file "map.data".* (5) Convert the read sparse array into a new chess array.*  * @author Zzay* @version 2021/01/14*/
public class SparseArray {public final static int ROW_SIZE = 11;public final static int COLUMN_SIZE = 11;public final static int NONE = 0;public final static int BLACK = 1;public final static int WHITE = 2;public final static String fileName = "map.data";public final static File destFile = new File(fileName);public final static File srcFile = new File(fileName);// The array representing the chessboard.private static int[][] chessArr = new int[ROW_SIZE][COLUMN_SIZE];// The sparse array to be formed.private static int[][] sparseArr;public static void main(String[] args) {initialize();createSparseArr();saveSparseArr();convertSparseArr(readSparseArr());}
}
  • Methods
    /*** The initialization of the chessArr.*/private static void initialize() {// Assign initial value for some elements.chessArr[1][2] = BLACK;chessArr[2][3] = WHITE;chessArr[3][4] = BLACK;chessArr[4][5] = WHITE;}/*** Create the sparse array of the chessArr, and print it.*/private static void createSparseArr() {// The total number of the effective element in the chessArr.int sum = 0;// Loop the chessArr and figure out the sum of practical elements.for (int[] rowArr : chessArr) {for (int data : rowArr) {if (data != NONE) {sum++;}}}// Create the sparseArr with the sum.// The three columns represent row, column and value respectively.sparseArr = new int[sum + 1][3];sparseArr[0][0] = ROW_SIZE;sparseArr[0][1] = COLUMN_SIZE;sparseArr[0][2] = sum;// Assign those elements' value into the sparseArr.// [ Tips: enhanced for loop cannot be used here,//  since the row and column index need to be known].int count = 0;for (int i = 0; i < chessArr.length; i++) {for (int j = 0; j < chessArr.length; j++) {if (chessArr[i][j] != NONE) {count++;sparseArr[count][0] = i;sparseArr[count][1] = j;sparseArr[count][2] = chessArr[i][j];}}}// Print the sparseArr.for (int[] rowArr : sparseArr) {for (int data : rowArr) {System.out.printf("%d\t", data);}System.out.println();}System.out.println();}/*** Save the sparse array into file "map.data".*/private static void saveSparseArr() {FileWriter fw;BufferedWriter bw = null;try {fw = new FileWriter(destFile);bw = new BufferedWriter(fw);for (int[] rowArr : sparseArr) {for (int data : rowArr) {bw.write(data + " ");}bw.write("\r\n");bw.flush();}} catch (IOException e) {e.printStackTrace();} finally {try {if (bw != null) {bw.close();}} catch (IOException e) {e.printStackTrace();}}}/*** Read the sparse array from file "map.data".*/private static int[][] readSparseArr() {int[][] sparseArr = new int[0][];boolean firstRowSet = false;FileReader fr;BufferedReader br = null;try {fr = new FileReader(srcFile);br = new BufferedReader(fr);int count = 0;String data;// Assign values to the following rows.while ((data = br.readLine()) != null) {String[] splitedData = data.split(" ");int rowNum = Integer.parseInt(splitedData[0]);int colNum = Integer.parseInt(splitedData[1]);int value = Integer.parseInt(splitedData[2]);// Initialize the first row of the sparse array.if (!firstRowSet) {sparseArr = new int[value + 1][3];firstRowSet = true;}sparseArr[count][0] = rowNum;sparseArr[count][1] = colNum;sparseArr[count][2] = value;count++;}} catch (IOException e) {e.printStackTrace();} finally {try {if (br != null) {br.close();}} catch (IOException e) {e.printStackTrace();}}return sparseArr;}/*** Convert the sparse array into a new chessArr.*/private static void convertSparseArr(int[][] readSparseArr) {// Get the original double-dimension array from the sparseArr.int rowNum = readSparseArr[0][0];int colNum = readSparseArr[0][1];int eleNum = readSparseArr[0][2];int[][] newArr = new int[rowNum][colNum];for (int i = 1; i <= eleNum; i++) { // Or "i < sparseArr.length"int row = readSparseArr[i][0];int col = readSparseArr[i][1];int value = readSparseArr[i][2];newArr[row][col] = value;}// Print the newArr.for (int[] rowArr : newArr) {for (int data : rowArr) {System.out.printf("%d\t", data);}System.out.println();}System.out.println();}

数据结构与算法--稀松数组(Sparse Array)相关推荐

  1. JavaScript数据结构和算法简述——数组

    为什么先讲数组 数据结构可以简单的被分为线性结构和非线性结构. 线性结构大致包括: 数组(连续存储): 链表(离散存储): 栈(线性结构常见应用,由链表或数组增删和改进功能实现): 队列(线性结构常见 ...

  2. 数据结构与算法---稀疏数组

    数据结构与算法-稀疏数组 1.基本介绍: ​ 当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组. 2.稀疏数组的处理方法是: ​ (1)记录数组一共有几行几列,有多少个 ...

  3. 数据结构与算法--有序数组中找出和为s的两个数字

    有序数组中找和为s的两个数字 题目:输入一个递增排序的数组array, 和一个数字s, 在数组中找出两个数,使得这两个数的和是s,如果有多对,输出一对即可. 最简单方案 双循环,每次获取一个数据,和数 ...

  4. 数据结构与算法--将数组排成最小的数

    将数组排成最小的数 题目:输入一个正整数的数组,将数组中所有数字拼接在一起排列成一个新的数,打印能拼接出来的所有数字中最小的一个, 案例:输入数组{12,4,55},则能打印出最小的数组是:12455 ...

  5. 【数据结构与算法】数组与链表

    数组的定义和特性 数组(Array)是一种线性表数据结构.它用一组连续的内存空间,来存储一组具有相同类型的数据. 线性表(Linear List):数组.链表.队列.栈 非线性表:树 图 连续的内存空 ...

  6. 数据结构与算法基础-数组

    原文:chenmingyu.top/data-struct- 数组 数组是用于储存多个相同类型数据的集合,使用一段连续的内存空间存储数据 数组作为最基本的数据结构,想必大家一定已经足够了解,数组的增删 ...

  7. 菜鸟窝-数据结构与算法之数组实现队列

    categories: 数据结构与算法 tags: 队列 title: 数据结构与算法之队列 date: 数据结构之队列 定义:有序列表,可以通过数组或者链表实现,遵循先入先出的原则 数组实现普通队列 ...

  8. [数据结构与算法] 串,数组和广义表

    串偏向于算法,数组和广义表偏向于理解 第四章 串.数组和广义表 4.1 串的定义 4.2 案例引入 4.3 串的类型定义,存储结构及运算 4.3.1 **串的类型定义** 4.3.2 串的存储结构 4 ...

  9. 408考研数据结构与算法之数组、链表、队列、栈知识点和算法详细教程(更新中)

    第一章:数据结构与算法概述 因为数据结构作为计算机专业的专业基础课程,是计算机考研的必考科目之一,如果打算报考计算机专业的研究生,你必须学好它. 数据结构是计算机软考.计算机等级考试等相关考试的必考内 ...

最新文章

  1. 怎么剪切一段音乐其中的片段
  2. SAP UI5 的本地 Fiori sandbox Launchpad
  3. ADO.NET- 基础总结及实例
  4. Android 利润,惊人利润:Android系统免费背后影藏的巨大利润
  5. java mail 已发送_JavaMail获取已发送邮件
  6. sql join语句语法_SQL Left Join语句:示例语法
  7. 华为服务器装系统怎么选pxe,华为TaiShan服务器PXE操作系统
  8. linux窗口黑边,解决deepin-scrot在gnome3截图顶部出现黑边问题
  9. [Ext JS 4] 实战之浏览器兼容
  10. win11HDMI端口无法使用怎么办 windows11HDMI端口无法使用的解决方法
  11. python跟我学_灞桥区跟我学python
  12. 正则表达式入门30分钟——很经典的入门教材
  13. linux下intel安装教程,在64位Ubuntu下安装Intel Fortran 11
  14. R语言统计—配对t检验样本量计算
  15. jenkins with ant 和 invoke ant
  16. 家用计算机按键不灵怎么修,空格键失灵了怎么办?电脑键盘按键失灵的解决办法...
  17. 互联网时代下的市场营销
  18. Java实现吸血鬼数字
  19. 国内网络环境安装QIIME2(100%成功)
  20. 圆周角、圆心角、弦、弦心距、弧长、扇形面积

热门文章

  1. 读《向上生长》的一点感触
  2. 抖音极速版/快手极速版自动浏览-实操记录
  3. 利用java的PriorityQueue类实现堆排序(java实现)
  4. 最新小程序源码(微信/抖音小程序源码和小程序游戏源码开发)
  5. 互联网巨头农业版图争夺战①——另辟蹊径的拼多多
  6. [Kindle]如何用邮箱发送文件至Kindle设备?
  7. 鸿蒙开发者公测版本bug,用华为鸿蒙看优酷无广告:明显是BUG,不是特意搞的噱头...
  8. Laravel修改配置后一定要清理缓存 php artisan config:clear!
  9. access团员人数公式_ACCESS选择模拟题
  10. Pandas-时间序列(二)-索引及切片:TimeSeries是Series的一个子类,所以Series索引及数据选取方面的方法基本一样【TimeSeries通过时间序列有更便捷的方法做索引和切片】