当前位置:网站首页>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
边栏推荐
- Presentation of attribute value of an item
- 博弈论 AcWing 892. 台阶-Nim游戏
- 7.Oracle-表结构
- 如何正确在CSDN问答进行提问
- 将webApp或者H5页面打包成App
- The route of wechat applet jumps again without triggering onload
- [learning] database: MySQL query conditions have functions that lead to index failure. Establish functional indexes
- Chinese remainder theorem acwing 204 Strange way of expressing integers
- Find the combination number acwing 888 Find the combination number IV
- Interval problem acwing 906 Interval grouping
猜你喜欢
高斯消元 AcWing 884. 高斯消元解异或線性方程組
Vant weave swipecell sets multiple buttons
Gauss Cancellation acwing 884. Solution d'un système d'équations Xor linéaires par élimination gaussienne
2021apmcm post game Summary - edge detection
Redis-01. First meet redis
Financial risk control practice -- feature derivation based on time series
博弈论 AcWing 891. Nim游戏
博弈论 AcWing 893. 集合-Nim游戏
NotImplementedError: Cannot convert a symbolic Tensor (yolo_boxes_0/meshgrid/Size_1:0) to a numpy ar
Find the combination number acwing 889 01 sequence meeting conditions
随机推荐
2021apmcm post game Summary - edge detection
阿里新成员「瓴羊」正式亮相,由阿里副总裁朋新宇带队,集结多个核心部门技术团队
Game theory acwing 892 Steps Nim game
PR automatically moves forward after deleting clips
SQL三种连接:内连接、外连接、交叉连接
4. Object mapping Mapster
Niu Mei's math problems
confidential! Netease employee data analysis internal training course, white whoring! (attach a data package worth 399 yuan)
Vant weapp swippecell set multiple buttons
4. Oracle redo log file management
如何正确在CSDN问答进行提问
区间问题 AcWing 906. 区间分组
What is socket? Basic introduction to socket
‘mongoexport‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
Game theory acwing 891 Nim games
[2021]IBRNet: Learning Multi-View Image-Based Rendering Qianqian
C job interview - casting and comparing - C job interview - casting and comparing
Record the process of configuring nccl and horovod in these two days (original)
3.Oracle-控制文件的管理
求组合数 AcWing 889. 满足条件的01序列