当前位置:网站首页>05 use of array
05 use of array
2022-04-23 07:49:00 【Night sky】
Sparse array
A two-dimensional array , Most of the values are defaults , A few cases with other values , Can be converted into sparse array storage , Reduce storage space , Time for space ,
The first row of the sparse array stores the size of the original array and the number of values
The remaining lines store the location information of the special value and the content of the value
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
The number of valid values :2
That's ok Column value
11 11 2
1 2 1
2 3 2
int[][] eleven = new int[11][11];
eleven[1][2] = 1;
eleven[2][3] = 2;
// Output original array
for (int[] ints : eleven) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
// Convert to a sparse array
int sum = 0;
for (int[] ints : eleven) {
for (int anInt : ints) {
if (anInt != 0) {
sum++;
}
}
}
System.out.println(" The number of valid values :"+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(" That's ok "+"\t");
System.out.print(" Column "+"\t");
System.out.print(" value "+"\t");
System.out.println();
for (int[] ints : trans) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
版权声明
本文为[Night sky]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230627211131.html
边栏推荐
猜你喜欢
随机推荐
MySQL8. 0 installation / uninstallation tutorial [window10 version]
MySQL storage engine
NodeJS(六) 子进程操作
js之什么是事件?事件三要素以及操作元素
MySQL in window10 version does not work after setting remote access permission
Window10版MySQL设置远程访问权限后不起效果
解决在docker中部署mysql8, 密码正确但无法登陆MySQL问题
Nodejs (II) read files synchronously and asynchronously
js之DOM学习获取元素
js中对象的三种创建方式
C#使用拉依达准则(3σ准则)剔除异常数据(.Net剔除一组数据中的奇异值)
unity UGUI判断点击在UI上和3D物体上的解决方案
颜色转换公式大全及转换表格(31种)
js之作用域、作用域链、全局变量和局部变量
C operation registry full introduction
Mongodb starts warning information processing
new的实现
大学学习路线规划建议贴
Super classic & Programming Guide (red and blue book) - Reading Notes
移动Web(字体图标、平面转换、颜色渐变)