当前位置:网站首页>05数组的使用
05数组的使用
2022-04-23 06:28:00 【夜夜夜空】
稀疏数组
一个二维数组,大部分的值都是默认值,少数有其他值的情况下,可以转化成稀疏数组存储,减少存储空间,以时间换空间,
稀疏数组的第一行存储原数组的大小和值的个数
剩下行存的就是特殊值的位置信息和值的内容
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
有效值的个数:2
行 列 值
11 11 2
1 2 1
2 3 2
int[][] eleven = new int[11][11];
eleven[1][2] = 1;
eleven[2][3] = 2;
//输出原始数组
for (int[] ints : eleven) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
//转换为稀疏数组
int sum = 0;
for (int[] ints : eleven) {
for (int anInt : ints) {
if (anInt != 0) {
sum++;
}
}
}
System.out.println("有效值的个数:"+sum);
int[][] trans = new int[sum+1][3];
trans[0][0] = 11;
trans[0][1] = 11;
trans[0][2] = sum;
int temp = 1;
for (int i = 0; i < eleven.length; i++) {
for (int j = 0; j < eleven[i].length; j++) {
if (eleven[i][j]!=0){
trans[temp][0] = i;
trans[temp][1] = j;
trans[temp][2] = eleven[i][j];
temp++;
}
}
}
System.out.print("行"+"\t");
System.out.print("列"+"\t");
System.out.print("值"+"\t");
System.out.println();
for (int[] ints : trans) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
版权声明
本文为[夜夜夜空]所创,转载请带上原文链接,感谢
https://blog.csdn.net/zixuanyankai/article/details/123249880
边栏推荐
- MySQL8.0 安装/卸载 教程【Window10版】
- Preliminary configuration of OpenGL super Dictionary (freeglut, glew, gltools, GLUT)
- Online Safe Trajectory Generation For Quadrotors Using Fast Marching Method and Bernstein Basis Poly
- NodeJS(一) 事件驱动编程
- js之作用域、作用域链、全局变量和局部变量
- SAP pi / PO rfc2soap publishes RFC interface as WS example
- js之预解析
- Nodejs (I) event driven programming
- Daily question | fear dominated by reverse linked list
- 平面定义-平面方程
猜你喜欢
对js中argumens的简单理解
Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
Super classic & Programming Guide (red and blue book) - Reading Notes
Simple random roll call lottery (written under JS)
SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)
Ogldev reading notes
SAP pi / PO rfc2restful publishing RFC interface is a restful example (proxy indirect method)
Dropping Pixels for Adversarial Robustness
Authorization+Token+JWT
Use of command line parameter passing library argparse
随机推荐
系统与软件安全研究(四)
RGB颜色转HEX进制与单位换算
TimelineWindow
Xamarin版的C# SVG路径解析器
系统与软件安全研究(一)
UnityShader基础
Mvcc (multi version concurrency control)
大学学习路线规划建议贴
SVG中年月日相关的表达式
SVG中Path Data数据简化及文件夹所有文件批量导出为图片
二叉树的深度
Use of command line parameter passing library argparse
C operation registry full introduction
js之什么是事件?事件三要素以及操作元素
canvas学习第一篇
移动端布局(3D转换、动画)
Redis connection error err auth < password > called without any password configured for the default user
Daily question | fear dominated by reverse linked list
NodeJS(六) 子进程操作
基于NLP的软件安全研究(一)