当前位置:网站首页>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
边栏推荐
- Vant weapp swippecell set multiple buttons
- 5. Oracle tablespace
- 中国剩余定理 AcWing 204. 表达整数的奇怪方式
- 2048项目实现
- Chinese remainder theorem acwing 204 Strange way of expressing integers
- Bit of MySQL_ OR、BIT_ Count function
- 时间很快,请多做有意义的事情
- Adg5412fbruz-rl7 applies dual power analog switch and multiplexer IC
- 5.Oracle-表空间
- Record of problems in ollvm compilation
猜你喜欢
Suppose a bank's ATM machine, which allows users to deposit and withdraw money. Now there is 200 yuan in an account, and both user a and user B have the right to deposit and withdraw money from this a
Operator priority, one catch, no doubt
Idea debug failed
MPLS experiment
Chinese remainder theorem acwing 204 Strange way of expressing integers
Simple selection sort of selection sort
Client use of Argo CD installation
Adg5412fbruz-rl7 applies dual power analog switch and multiplexer IC
Vant Weapp SwipeCell設置多個按鈕
容斥原理 AcWing 890. 能被整除的数
随机推荐
Find the combination number acwing 887 Find combination number III
Applicable to Net free barcode API [off] - free barcode API for NET [closed]
How to correctly ask questions in CSDN Q & A
confidential! Netease employee data analysis internal training course, white whoring! (attach a data package worth 399 yuan)
高斯消元 AcWing 884. 高斯消元解异或線性方程組
[BMZCTF-pwn] ectf-2014 seddit
MPLS experiment
[2021]GIRAFFE: Representing Scenes as Compositional Generative Neural Feature Fields
什么是套接字?Socket基本介绍
Time is fast, please do more meaningful things
Configuration method and configuration file of SolidWorks GB profile library
2. Addition and management of Oracle data files
4.Oracle-重做日志文件管理
微信小程序路由再次跳轉不觸發onload
The “mode“ argument must be integer. Received an instance of Object
2022-5-第四周日报
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
博弈论 AcWing 893. 集合-Nim游戏
高斯消元 AcWing 884. 高斯消元解异或线性方程组
[Gaode map POI stepping pit] amap Placesearch cannot be used