当前位置:网站首页>稀疏数组(非线性结构)
稀疏数组(非线性结构)
2022-07-02 06:09:00 【我觉得海星_98】
当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。
转化过程
五子棋棋盘 > 二维数组 > 稀疏数组 > 存储
图解
- 11*11的二维数组,其中1的位置为[1] [2],2的位置为[2] [3]
- 3*3的稀疏二维数组,其中首行数据为 行列值 和 非零值的个数,后几行数据为 存储的非零值

代码
注意看注释的思路讲解~
package com.atguigu.sparsearray;
//author qij
public class SparseArray {
public static void main(String[] args) {
// 创建一个原始的二维数组 11 * 11
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];
//3.给稀疏数组的首行数据赋值
sparseArr[0][0] = 11;
sparseArr[0][1] = 11;
sparseArr[0][2] = sum;
//4.遍历二维数组,将非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. 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组
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();
}
}
}
输出结果:


边栏推荐
- 让每一位开发者皆可使用机器学习技术
- Servlet web XML configuration details (3.0)
- Sumo tutorial Hello World
- 社区说|Kotlin Flow 的原理与设计哲学
- Common means of modeling: combination
- Deep learning classification network -- Network in network
- Scheme and implementation of automatic renewal of token expiration
- LeetCode 27. Removing Elements
- Browser principle mind map
- 加密压缩文件解密技巧
猜你喜欢

加密压缩文件解密技巧

Web components series (VIII) -- custom component style settings

Invalid operation: Load into table ‘sources_orderdata‘ failed. Check ‘stl_load_errors‘ system table

Leverage Google cloud infrastructure and landing area to build enterprise level cloud native excellent operation capability

深入了解JUC并发(二)并发理论

【C语言】简单实现扫雷游戏

深入学习JVM底层(四):类文件结构

谷歌出海创业加速器报名倒计时 3 天,创业人闯关指南提前收藏!

复杂 json数据 js前台解析 详细步骤《案例:一》

Eco express micro engine system has supported one click deployment to cloud hosting
随机推荐
[C language] screening method for prime numbers
来自读者们的 I/O 观后感|有奖征集获奖名单
神机百炼3.54-染色法判定二分图
New version of dedecms collection and release plug-in tutorial tool
Problems encountered in uni app development (continuous update)
State machine in BGP
Singleton mode compilation
官方零基础入门 Jetpack Compose 的中文课程来啦!
网络相关知识(硬件工程师)
线性dp(拆分篇)
Reading classic literature -- Suma++
Deep learning classification network -- Network in network
Contest3145 - the 37th game of 2021 freshman individual training match_ H: Eat fish
【C语言】筛选法求素数
MUI底部导航的样式修改
BGP 路由优选规则和通告原则
使用HBuilderX的一些常用功能
Stc8h8k series assembly and C51 actual combat - digital display ADC, key serial port reply key number and ADC value
Contest3147 - game 38 of 2021 Freshmen's personal training match_ G: Flower bed
LeetCode 83. 删除排序链表中的重复元素