当前位置:网站首页>Mutual transformation between two-dimensional array and sparse array (sparse matrix)
Mutual transformation between two-dimensional array and sparse array (sparse matrix)
2022-07-05 06:35:00 【Said it was all】
What is a sparse array , Look at the picture
Convert to sparse array
The following code is used to realize the mutual transformation of array and sparse array ( Dynamic , You can modify the storage elements of the original array at will ):
public class day1Test01 {
public static void main(String[] args) {
int[][] arr = new int[6][6];
int sum = 0;
arr[0][0] = 1;
arr[1][1] =2;
arr[3][3]=4;
arr[2][1]=3;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
if ((arr[i][j]) != 0) {
sum = sum + 1;
}
System.out.printf("%d\t",arr[i][j]);
}
System.out.println();
}
//======================================================================
System.out.println(" The two-dimensional array is converted into a sparse array and the conversion begins ");
int[][] xishuarr = new int[sum + 1][3];
xishuarr[0][0] = arr.length;
xishuarr[0][1] = arr[0].length;
xishuarr[0][2] = sum;
int a=0;
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[0].length; j++) {
if ((arr[i][j]) != 0) {
a=a+1;
xishuarr[a][2] = arr[i][j];
xishuarr[a][0] = i;
xishuarr[a][1] = j;
}
}
}
for (int m = 0; m < xishuarr.length; m++) {
for (int n = 0; n < xishuarr[0].length; n++) {
System.out.printf("%d\t",xishuarr[m][n]);
}
System.out.println();
}
// Convert sparse array to two-dimensional array
System.out.println(" Sparse arrays begin to be converted into two-dimensional arrays ");
int arr2[][]=new int [xishuarr[0][0]][xishuarr[0][1]];
int resum=xishuarr[0][2];
for (int i=0;i<resum;i++){
arr2[xishuarr[i+1][0]][xishuarr[i+1][1]]=xishuarr[i+1][2];
}
for (int [] row :arr2){
for (int data:row){
System.out.printf("%d\t",data);
}
System.out.println();
}
}
}
** Output is as follows **
Two dimensional array traversal results :
1 0 0 0 0 0
0 2 0 0 0 0
0 3 0 0 0 0
0 0 0 4 0 0
0 0 0 0 0 0
0 0 0 0 0 0
The two-dimensional array is converted into a sparse array and the conversion begins
6 6 4
0 0 1
1 1 2
2 1 3
3 3 4
Sparse arrays begin to be converted into two-dimensional arrays
1 0 0 0 0 0
0 2 0 0 0 0
0 3 0 0 0 0
0 0 0 4 0 0
0 0 0 0 0 0
0 0 0 0 0 0
边栏推荐
- TypeScript入门
- 2022 winter vacation training game 5
- TCP's understanding of three handshakes and four waves
- 5.Oracle-錶空間
- Gaussian elimination acwing 884 Gauss elimination for solving XOR linear equations
- P3265 [jloi2015] equipment purchase
- Record of problems in ollvm compilation
- Chinese remainder theorem acwing 204 Strange way of expressing integers
- Bash exercise 17 writing scripts to install the server side of FRP reverse proxy software
- 代码中的英语全部
猜你喜欢
[Gaode map POI stepping pit] amap Placesearch cannot be used
博弈论 AcWing 892. 台阶-Nim游戏
Install opencv -- CONDA to establish a virtual environment and add the kernel of this environment in jupyter
什么是套接字?Socket基本介绍
What is linting
[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
7.Oracle-表结构
5. Oracle TABLESPACE
[wustctf2020] plain_ WP
LSA Type Explanation - lsa-1 [type 1 LSA - router LSA] detailed explanation
随机推荐
Vant weapp swippecell set multiple buttons
[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
__ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
ollvm编译出现的问题纪录
LSA Type Explanation - detailed explanation of lsa-2 (type II LSA network LSA) and lsa-3 (type III LSA network Summary LSA)
Applicable to Net free barcode API [off] - free barcode API for NET [closed]
There are three kinds of SQL connections: internal connection, external connection and cross connection
5.Oracle-錶空間
Single chip computer engineering experience - layered idea
Time is fast, please do more meaningful things
P3265 [jloi2015] equipment purchase
容斥原理 AcWing 890. 能被整除的数
微信小程序路由再次跳轉不觸發onload
[learning] database: MySQL query conditions have functions that lead to index failure. Establish functional indexes
Redis-02. Redis command
La redirection de l'applet Wechat ne déclenche pas onload
3. Oracle control file management
[BMZCTF-pwn] ectf-2014 seddit
背包问题 AcWing 9. 分组背包问题
2048项目实现