当前位置:网站首页>稀疏数组sparsearray
稀疏数组sparsearray
2022-06-26 06:12:00 【Oh No 发量又少了】
1.基本介绍
当一个数组中大部分元素为0,或者为同一个值的数组时,可以使用稀疏数组来保存该数组。
稀疏数组的处理方法是:
- 记录数组一共有几行几列,有多少个不同的值
- 把具有不值的元素的行列及值记录在一个小规模的数组中,从而缩小程序的规模
2.转换思路
二维数组转稀疏数组的思路
- 遍历原始的二维数组,得到有效数的个数sum
- 根据sum就可以创建稀疏数组sparseArr int[sum+1][3]
- 将二维数组的有效数据存入到稀疏数组
稀疏数组转原始二维数组的思路 - 先读取稀疏数组的第一行,根据第一行的数据,创建原始的二维数组,比如上面的chessArr2 = int [11][11]
- 在读取稀疏数组后几行的数据,并赋给原始的二维数组即可。
3. 代码实现
原始二位数组转稀疏数组
- 先创建原始二维数组
- 原始二维数组转稀疏数组
a. 先遍历二维数组,得到非零数据的个数sum
b. 创建稀疏数组
c. 给稀疏数组赋值
d. 将二维数组中非零数据存入二维数组
package the10sessionA.test1.chessArray;
//稀疏数组与普通二维数组之间的转换
public class Main {
public static void main(String[] args) {
//1.先创建一个普同的二维数组
int array1 [][] = new int[11][11];
array1 [5][6] = 1;
array1 [6][5] = 2;
//2.便利普通二维数组记录非零数据的个数sum
int sum = 0;
int x = array1.length;
int y = 0;
for (int [] data : array1) {
for (int i : data) {
if (i != 0) {
sum++;
}
}
y = data.length;
}
System.out.println("得sum="+sum);
int chessArray [][] = new int[sum+1][3];
chessArray [0][0] = x;
chessArray [0][1] = y;
chessArray [0][2] = sum;
int temp = 1;
for (int i = 0; i < x; i++) {
for (int j = 0; j < y; j++) {
if (array1[i][j] != 0) {
chessArray[temp][0] = i;
chessArray[temp][1] = j;
chessArray[temp][2] = array1[i][j];
temp++;
}
}
}
System.out.println("输出chessArray数组");
for (int [] data : chessArray) {
for (int i : data) {
System.out.print(i+" ");
}
System.out.println();
}
//稀疏数组转二维数组
int array2 [][] = new int[chessArray [0][0]][chessArray [0][1]];
for (int i = 1; i < chessArray.length; i++) {
array2[chessArray[i][0]][chessArray[i][1]] = chessArray[i][2];
}
//输出有稀疏数组转换而来的二维数组
for (int [] data : array2) {
for (int i : data) {
System.out.print(i+" ");
}
System.out.println();
}
}
}
边栏推荐
- The purpose of writing programs is to solve problems
- Logstash - logstash sends an alarm email to email
- Playing RTSP streaming video on Web pages (webrtc)
- numpy. random. choice
- Logstash——Logstash向Email发送告警邮件
- Five solutions across domains
- MySQL-08
- 工作积累——Web请求中使用ThreadLocal遇见的问题
- Comparison between Prometheus and ZABBIX
- Class and object learning
猜你喜欢

University Information Management System

Detailed explanation of serial port communication principle 232, 422, 485

DS18B20详解

Import export simple

事务与消息语义

TCP連接與斷開,狀態遷移圖詳解

数据可视化实战:实验报告

Install pyinstaller

ByteDance starts the employee's sudden wealth plan and buys back options with a large amount of money. Some people can earn up to 175%

在web页面播放rtsp流视频(webrtc)
随机推荐
numpy.exp()
Class and object learning
numpy.frombuffer()
MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications
技术能力的思考和总结
SQL Server view
Logstash——Logstash将数据推送至Redis
Mysql-10 (key)
Volatile application scenarios
Logstash——Logstash向Email发送告警邮件
[spark] how to implement spark SQL field blood relationship
Solve the problem that Cmdr cannot use find command under win10
Architecture design method
Cython入门
Multi thread synchronous downloading of network pictures
Household accounting procedures (the second edition includes a cycle)
Design and practice of low code real-time data warehouse construction system
05. basic data type - Dict
Playing RTSP streaming video on Web pages (webrtc)
The difference between abstract and interface interface