当前位置:网站首页>稀疏数组(非线性结构)
稀疏数组(非线性结构)
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();
}
}
}
输出结果:


边栏推荐
- Deep learning classification network -- Network in network
- BGP报文详细解释
- Verifying downloaded files using sha256 files
- 线性dp(拆分篇)
- Sumo tutorial Hello World
- 如何使用MITMPROXy
- Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
- 步骤详解 | 助您轻松提交 Google Play 数据安全表单
- Memcached installation
- Lambda expressions and method references
猜你喜欢

Happy Lantern Festival | Qiming cloud invites you to guess lantern riddles

Detailed notes of ES6

WLAN相关知识点总结

Little bear sect manual query and ADC in-depth study

VRRP之监视上行链路

CNN visualization technology -- detailed explanation of cam & grad cam and concise implementation of pytorch

ROS2----LifecycleNode生命周期节点总结

线性dp(拆分篇)

BGP中的状态机

Linkage between esp8266 and stc8h8k single chip microcomputer - Weather Clock
随机推荐
Verifying downloaded files using sha256 files
External interrupts cannot be accessed. Just delete the code and restore it Record this unexpected bug
借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
IPv6 experiment and summary
Lucene Basics
492. Construction rectangle
数据回放伴侣Rviz+plotjuggler
BGP 路由优选规则和通告原则
State machine in BGP
Cookie plugin and localforce offline storage plugin
LeetCode 27. 移除元素
BGP中的状态机
Redis key value database [advanced]
Page printing plug-in print js
神机百炼3.54-染色法判定二分图
神机百炼3.52-Prim
On Web server
Contest3147 - game 38 of 2021 Freshmen's personal training match_ E: Listen to songs and know music
Google play academy team PK competition, official start!
Introduce uview into uni app