当前位置:网站首页>1.1、稀疏数组
1.1、稀疏数组
2022-07-28 17:05:00 【TUJC】
线性结构
- 1)线性结构作为最常用的数据结构, 其特点是数据元素之间存在一对一的线性关系;
- 2)线性结构有两种不同的存储结构, 即顺序存储结构(数组)和链式存储结构(链表)。顺序存储的线性表称为顺序 表,顺序表中的存储元素是连续的
- 3)链式存储的线性表称为链表, 链表中的存储元素不一定是连续的, 元素节点中存放数据元素以及相邻元素的地 址信息
- 4)线性结构常见的有:数组,队列,链表和栈,后面我们会详细讲解.
非线性结构
非线性结构包括: 二维数组,多维数组,广义表,树结构,图结构
1.1、稀疏sparsearray 数组
1.1.1、先看一个实际的需求
编写的五子棋程序中,有存盘退出和续上盘的功能。

分析问题:
因为该二维数组的很多值是默认值0,因此记录了很多没有意义的数据.->稀疏数组。
1.1.2、基本介绍
当一个数组中大部分元素为0,或者为同一个值的数组时,可以用稀疏数组来保存该数组。
稀疏数组的处理方法是:
- 1)记录数组一共有几行几列,有多少个不同的值
- 2)把具有不同值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模

1.1.3应用实例
- 1)使用稀疏数组,来保留类似前面的二维数组(棋盘、地图等等)
- 2)把稀疏数组存盘,并且可以从新恢复原来的二维数组数
- 3)整体思路分析:

- 4)代码实现
public class SparseArray {
public static void main(String[] args) {
// 创建一个原始的二维数组 11 * 11
// 0: 表示没有棋子, 1 表示 黑子 2 表蓝子
int chessArr1[][] = new int[11][11];
chessArr1[1][2] = 1;
chessArr1[2][3] = 2;
chessArr1[4][5] = 2;
// 输出原始的二维数组
System.out.println("原始的二维数组~~");
for (int[] row : chessArr1) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
// 将二维数组 转 稀疏数组的思
// 1. 先遍历二维数组 得到非0数据的个数
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr1[i][j] != 0) {
sum++;
}
}
}
// 2. 创建对应的稀疏数组
int sparseArr[][] = new int[sum + 1][3];
// 给稀疏数组赋值
sparseArr[0][0] = 11;
sparseArr[0][1] = 11;
sparseArr[0][2] = sum;
// 遍历二维数组,将非0的值存放到 sparseArr中
int count = 0; //count 用于记录是第几个非0数据
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (chessArr1[i][j] != 0) {
count++;
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = chessArr1[i][j];
}
}
}
// 输出稀疏数组的形式
System.out.println();
System.out.println("得到稀疏数组为~~~~");
for (int i = 0; i < sparseArr.length; i++) {
System.out.printf("%d\t%d\t%d\t\n", sparseArr[i][0], sparseArr[i][1], sparseArr[i][2]);
}
System.out.println();
//将稀疏数组 --》 恢复成 原始的二维数组
/* * 1. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,比如上面的 chessArr2 = int [11][11] 2. 在读取稀疏数组后几行的数据,并赋给 原始的二维数组 即可. */
//1. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组
int chessArr2[][] = new int[sparseArr[0][0]][sparseArr[0][1]];
//2. 在读取稀疏数组后几行的数据(从第二行开始),并赋给 原始的二维数组 即可
for(int i = 1; i < sparseArr.length; i++) {
chessArr2[sparseArr[i][0]][sparseArr[i][1]] = sparseArr[i][2];
}
// 输出恢复后的二维数组
System.out.println();
System.out.println("恢复后的二维数组");
for (int[] row : chessArr2) {
for (int data : row) {
System.out.printf("%d\t", data);
}
System.out.println();
}
}
}
边栏推荐
- What is the employment prospect of software testing?
- @Autowired与@Resource区别
- 记录自己在厦门两年来的面试经历--完结篇
- Leetcode79 method 1: deep search
- 不理解模块化、组件化、插件化的区别怎么行?
- DC-DC开关电源
- Performance parameters of spectrum analyzer
- 实验楼----PHP大法
- UE5 GAS 学习笔记0.2配置插件
- Ue5 gas learning notes 1.9 skill system global classes (abilitysystemglobals)
猜你喜欢

直播|StarRocks 技术内幕 :低基数全局字典优化

Internet intelligence, how to define the next generation network transformation

ERROR 2003 (HY000) Can‘t connect to MySQL server on ‘localhost3306‘ (10061)解决办法

Calibration of vector network analyzer (vector network)

Golang并发模型之

Mqtt over quic: the next generation Internet of things standard protocol injects new impetus into the message transmission scenario

Multithreading and high concurrency -- source code analysis AQS principle

Brief introduction to the principle of spectrometer II

SQL Server stuff and for XML path

天线的原理、分类及要求
随机推荐
苹果开发完整的苹果证书与描述文件创建流程
Performance parameters of spectrum analyzer
Bubble sorting and Related videos
NDK series (5): from introduction to practice, JNI explodes the liver and explains everything in detail!
一文简述:SRv6基本原理
UE5 GAS 学习笔记8.0参考资料
Go并发详解之一
408复习策略(强化阶段)
数字化转型中的DevOps——弹性合作
There is a special cryptology language called asn.1
Ue5 gas learning notes 1.10 prediction
.net WCF wf4.5 state machine, bookmark and persistence
Detailed explanation of network RJ45 interface
Wired: who owns the art of the future? Openai allows dall-e users to commercialize their works. At present
.net swagger
UE5 GAS 学习笔记0.1 案例预览
UE5 GAS 学习笔记 1.7 任务Ability Tasks
Ue5 gas learning notes 1.7 task ability tasks
Digital torrent: resource reorganization and strategic conflict in enterprise transformation
UE5 GAS 学习笔记 1.2游戏标签