热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

稀松数组

稀松数组1.稀松数组什么?在一个数组中,若数值为0的元素数目远远多于非0元素的数目,并且非0元素分布没有规律时,则称该数组为稀疏数组;如图,一个5*5的数组arr上只有3个有效数值

稀松数组

1.稀松数组什么?

在一个数组中,若数值为0的元素数目远远多于非0元素的数目,并且非0元素分布没有规律时,则称该数组为稀疏数组;

如图,一个5*5的数组arr上只有3个有效数值其他22个位置全为无效数值

技术分享图片


2.为什么需要压缩稀松数组?

稀松数组中存在大量的无效数据,占用了很多没有必要占用的空间,使用压缩后就能节省空间。


3.怎么压缩稀松数组?

压缩稀松数组步骤如下:



  • 1.我们得先求出稀松数组有多少个有效数值sum

    遍历数组我们可以得到有效个数为3



  • 2.创建一个sum+1行,3列的数组

    创建一个4*3的数组



  • 3.根据稀松数组往创建好的压缩数组中填值



    • 3.1第一行分别填稀松数组的行,列,有效数值个数

      稀松数组的行为arr.length,列为arr[0].length,有效数值为3



    • 3.2其他位置填稀松数组每个有效值对应的行,列,以及具体的值





经过以上步骤我们就得到了如图所示的压缩过后的数组,可以看到占用的空间从原来的5x5变为了现在都3x4

技术分享图片


4.代码实现

既然压缩稀松数组可以节省空间,那我们来代码实现一下,这里我使用的是java代码


4.1.压缩稀松数组

//1.初始化一个稀松数组并存值
int[][] chessArr = new int[5][5];
chessArr[1][2] = 1;
chessArr[2][3] = 2;
chessArr[4][1] = 2;
//打印稀松数组
System.out.println("打印稀松数组:");
for (int arr[] : chessArr) {
for (int data :
arr) {
System.out.printf("%d\t", data);
}
System.out.println();
}
//2.将稀松数组进行压缩
//2.1统计稀松数组的有效值个数
int sum = 0;
for (int arr[] : chessArr) {
for (int data :
arr) {
if (data != 0) {
sum++;
}
}
}
//3创建压缩后的数组
int[][] sparseArray = new int[sum + 1][3];
//3.1为压缩后的数组中第一行填值
sparseArray[0][0] = chessArr.length;
sparseArray[0][1] = chessArr[0].length;
sparseArray[0][2] = sum;
//3.2为其他位置填值
int count = 0;
for (int i = 0; i for (int j = 0; j if (chessArr[i][j] != 0) {
count++;
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = chessArr[i][j];
}
}
}
System.out.println("压缩后的数组如下:");
for (int[] arr :
sparseArray) {
for (int data :
arr) {
System.out.printf("%d\t", data);
}
System.out.println();
}

运行结果:

技术分享图片


4.2.将压缩后的数组存储到磁盘

//创建一个文件
File file = new File("map.data");
//创建流
FileOutputStream fileOutputStream = new FileOutputStream(file);
//创建一个容器用来临时存储数组每一行的数据,这个地方直接用String是一样的
StringBuilder sb = new StringBuilder();
for (int i = 0; i for (int j = 0; j //如果当前的值为一行的最后
if (j == sparseArray[0].length - 1) {
//在容器后加入一个换行
sb.append(sparseArray[i][j] + "\n");
//转换为字节数组
byte[] bytes = sb.toString().getBytes();
//写入到文件中
fileOutputStream.write(bytes, 0, bytes.length);
//将容器清空
sb.delete(0, sb.length());
break;
}
sb.append(sparseArray[i][j] + ",");
}
}
fileOutputStream.close();//关闭流

运行结果:

技术分享图片


4.3.将磁盘中压缩后的数组还原为稀松数组

//创建文件
File mapFile = new File("map.data");
//拿到文件的字符流
FileReader fileReader = new FileReader(mapFile);
//加入缓冲流
BufferedReader bufferedReader = new BufferedReader(fileReader);
//从文件中读取一行
String s = bufferedReader.readLine();
//将读取的文本按照符号‘,‘进行分割
String[] split = s.split(",");
//创建稀松数组
int[][] chessArr2 = new int[Integer.parseInt(split[0])][Integer.parseInt(split[1])];
//往稀松数组中填值
while (true) {
s = bufferedReader.readLine();
if (s == null) {
//关闭流
bufferedReader.close();
break;
}
split = s.split(",");
chessArr2[Integer.parseInt(split[0])][Integer.parseInt(split[1])] = Integer.parseInt(split[2]);
}
System.out.println("通过文件流还原的数组:");

for (int[] arr :
chessArr2) {
for (int data :
arr) {
System.out.printf("%d\t", data);
}
System.out.println();
}

运行结果:

技术分享图片


4.4完整代码:

package com.gong.sparsearray;
import java.io.*;
/**
* @author 龚成龙
*/
public class SparseArray {
public static void main(String[] args) throws IOException {
//1.初始化一个稀松数组并存值
int[][] chessArr = new int[5][5];
chessArr[1][2] = 5;
chessArr[2][3] = 4;
chessArr[4][1] = 2;
//打印稀松数组
System.out.println("打印稀松数组:");
for (int arr[] : chessArr) {
for (int data :
arr) {
System.out.printf("%d\t", data);
}
System.out.println();
}
//2.将稀松数组进行压缩
//2.1统计稀松数组的有效值个数
int sum = 0;
for (int arr[] : chessArr) {
for (int data :
arr) {
if (data != 0) {
sum++;
}
}
}
//3创建压缩后的数组
int[][] sparseArray = new int[sum + 1][3];
//3.1为压缩后的数组中第一行填值
sparseArray[0][0] = chessArr.length;
sparseArray[0][1] = chessArr[0].length;
sparseArray[0][2] = sum;
//3.2为其他位置填值
int count = 0;
for (int i = 0; i for (int j = 0; j if (chessArr[i][j] != 0) {
count++;
sparseArray[count][0] = i;
sparseArray[count][1] = j;
sparseArray[count][2] = chessArr[i][j];
}
}
}
System.out.println("压缩后的数组如下:");
for (int[] arr :
sparseArray) {
for (int data :
arr) {
System.out.printf("%d\t", data);
}
System.out.println();
}
//4.将压缩后的数组存储在文件中
//创建一个文件
File file = new File("map.data");
//创建流
FileOutputStream fileOutputStream = new FileOutputStream(file);
//创建一个容器用来临时存储数组每一行的数据,这个地方直接用String是一样的
StringBuilder sb = new StringBuilder();
for (int i = 0; i for (int j = 0; j //如果当前的值为一行的最后
if (j == sparseArray[0].length - 1) {
//在容器后加入一个换行
sb.append(sparseArray[i][j] + "\n");
//转换为字节数组
byte[] bytes = sb.toString().getBytes();
//写入到文件中
fileOutputStream.write(bytes, 0, bytes.length);
//将容器清空
sb.delete(0, sb.length());
break;
}
sb.append(sparseArray[i][j] + ",");
}
}
fileOutputStream.close();//关闭流
//将磁盘中压缩后的数组还原为稀松数组
//创建文件
File mapFile = new File("map.data");
//拿到文件的字符流
FileReader fileReader = new FileReader(mapFile);
//加入缓冲流
BufferedReader bufferedReader = new BufferedReader(fileReader);
//从文件中读取一行
String s = bufferedReader.readLine();
//将读取的文本按照符号‘,‘进行分割
String[] split = s.split(",");
//创建稀松数组
int[][] chessArr2 = new int[Integer.parseInt(split[0])][Integer.parseInt(split[1])];
//往稀松数组中填值
while (true) {
s = bufferedReader.readLine();
if (s == null) {
//关闭流
bufferedReader.close();
break;
}
split = s.split(",");
chessArr2[Integer.parseInt(split[0])][Integer.parseInt(split[1])] = Integer.parseInt(split[2]);
}
System.out.println("通过文件流还原的数组:");
for (int[] arr :
chessArr2) {
for (int data :
arr) {
System.out.printf("%d\t", data);
}
System.out.println();
}
}
}


推荐阅读
  • iOS Auto Layout Demystify
    BookDescripterAutoLayouttransformsthewayyoucreateiOSuserinterfaces.Asflexibleasitispowerfu ... [详细]
  • String字符串java.lang;基本标识Java字符串的一个重要特点就是字符串不可变。finalclassString没有子类字符串字面量也是一个String类的实例存储在字 ... [详细]
  • 1011-MarriageCeremoniesPDF(English)StatisticsForumTimeLimit:2second(s)MemoryLimit:32MBYouw ... [详细]
  • 726:ROADS726:ROADS总时间限制:1000ms内存限制:65536kB描述Ncitiesnamedwithnumbers1Nareconnectedwithon ... [详细]
  • ProblemDescription:Readtheprogrambelowcarefullythenanswerthequestion.#pragmacomment(linker ... [详细]
  • AtCoder Beginner Contest 176   EBomber   (思维)
    题意:有一张$H$x$W$的图,给你$M$个目标的位置,你可以在图中放置一枚炸弹,炸弹可以摧毁所在的那一行和一列,问最多可以摧毁多少目标.题解:首先我们记录某一行和某一列目标最多的 ... [详细]
  • -(void)drawRect:(CGRect)rect{获得当前上下文CGContextRefctxUIGraphicsGetCurrentContext();把当前上下文状态保 ... [详细]
  • diskmark使用教程
    首先说明一下软件各个参数的意义。1~9测试次数;50MB~4000MB测试规模;C,D,E,F选择测试对象;ALL测试以下所有;第一行代表你硬盘的读写速度。第二行代表你硬盘4K文件 ... [详细]
  • #include#includeintmain(){printf(floorof2.3is%d\n,(int)floor(2.3));printf(floorof ... [详细]
  • 一步一步学EF系列【4、升级篇 实体与数据库的映射】
    之前的三张为基础篇,如果不考虑架构问题,做一般的小程序,以足够用了。基本的增删改查也都有了。但是作为学习显然是不够的。通过之前三章的学习,有没有发现这样写有什么问题,有没有觉得繁琐 ... [详细]
  • python+selenium+chrome网页自动化测试:1、在pycharm中安装selenium:file-setting-projectinterpreter中搜索sele ... [详细]
  • IE下PHPiframe跨域导致session丢失问题的解决方法|一个登录页面,被别的网站用iframe嵌进去后,死活无法登录(只在IE中存在这种情况)。主要是session无 ... [详细]
  • HDNoip201505宽宽的笔记难度级别:B;运行时间限制:1000ms;运行空间限制:51200KB;代码长度限制:2000000B试题描述宽宽是一个很萌很萌的爱记笔记的好孩子 ... [详细]
  • SoL:裸的次小生成树。。。推荐:KuangBin巨巨的博客模版+详解   http:www.cnblogs.comkuangbinp3147329.html# ... [详细]
  • 在action中,默认的是调用execute()方法,如果想处理多个业务逻辑的话,可以在action类中写很多个类似execute方法,然后再在struts.xml中配置actio ... [详细]
author-avatar
05358
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有