当前位置:网站首页>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
边栏推荐
猜你喜欢

Configure NPM

Window10版MySQL设置远程访问权限后不起效果

C# 多个矩形围成的多边形标注位置的问题

Online Safe Trajectory Generation For Quadrotors Using Fast Marching Method and Bernstein Basis Poly

js之排他思想及案例

Shapley Explanation Networks

H5 local storage data sessionstorage, localstorage

Authorization server (simple construction of authorization server)

Install and configure Taobao image NPM (cnpm)

'NPM' is not an internal or external command, nor is it a runnable program or batch file
随机推荐
C#使用拉依达准则(3σ准则)剔除异常数据(.Net剔除一组数据中的奇异值)
RGB颜色转HEX进制与单位换算
Game assisted script development journey
unity 屏幕自适应
Understanding the Role of Individual Units in a Deep Neural Networks(了解各个卷积核在神经网络中的作用)
SAP PI/PO功能运行状态监控检查
平面定义-平面方程
What are the total number of all courses of Mr. Tang Xiaoyang, who is very popular in CSDN (question mark)
C# SmoothProgressBar自定义进度条控件
Unity screen adaptation
快排的练习
移动布局(flex布局、视口标签)
How to judge whether a point is within a polygon (including complex polygons or a large number of polygons)
MySQL index
SampleCameraFilter
Nodejs (II) read files synchronously and asynchronously
keytool: command not found
Ogldev reading notes
js中对象的三种创建方式
系统与软件安全研究(五)