当前位置:网站首页>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
边栏推荐
- ABAP 7.4 SQL Window Expression
- H5 local storage data sessionstorage, localstorage
- Thorough inquiry -- understanding and analysis of cocos2d source code
- Judge whether the beginning and end of the string contain target parameters: startswith() and endswith() methods
- [self motivation series] what really hinders you?
- 移动端布局(3D转换、动画)
- Super classic & Programming Guide (red and blue book) - Reading Notes
- FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning
- 二叉树的深度
- SAP PI / Po rfc2restful Publishing RFC interface as restful examples (proxy indirect)
猜你喜欢
keytool: command not found
js之排他思想及案例
King glory - unity learning journey
Ogldev reading notes
MySQL index
SAP 03-AMDP CDS Table Function using ‘WITH‘ Clause(Join子查询内容)
对复杂字典Dictionary&lt;T1,T2&gt;排序问题
将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)
js之什么是事件?事件三要素以及操作元素
Custom time format (yyyy-mm-dd HH: mm: SS week x)
随机推荐
使用flask时代码无报错自动结束,无法保持连接,访问不了url。
RGB颜色转HEX进制与单位换算
BTREE, B + tree and hash index
NodeJS(一) 事件驱动编程
Judge whether the beginning and end of the string contain target parameters: startswith() and endswith() methods
快排的练习
设置了body的最大宽度,但是为什么body的背景颜色还铺满整个页面?
unity UGUI判断点击在UI上和3D物体上的解决方案
Implementation of MySQL persistence
MySQL index
将单行文字自动适应到目标矩形框内
Understanding the Role of Individual Units in a Deep Neural Networks(了解各个卷积核在神经网络中的作用)
反转链表练习
js之自定义属性以及H5中如何判断自定义属性
.NET 5 的新功能 What‘s new in .NET 5
Understanding of STL container
Protobuf 使用
Mvcc (multi version concurrency control)
SampleCameraFilter
keytool: command not found