当前位置:网站首页>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
边栏推荐
- ‘mongoexport‘ 不是内部或外部命令,也不是可运行的程序 或批处理文件。
- MySQL怎么运行的系列(八)14张图说明白MySQL事务原子性和undo日志原理
- H5 embedded app adapts to dark mode
- 代码中的英语全部
- 2.Oracle-数据文件的添加及管理
- Ffmpeg build download (including old version)
- Huawei bracelet, how to add medicine reminder?
- Vant Weapp SwipeCell设置多个按钮
- How to answer when you encounter a jet on CSDN?
- 5.Oracle-錶空間
猜你喜欢
MPLS experiment
Inclusion exclusion principle acwing 890 Divisible number
What is linting
'mongoexport 'is not an internal or external command, nor is it a runnable program or batch file.
ADG5412FBRUZ-RL7应用 双电源模拟开关和多路复用器IC
Game theory acwing 893 Set Nim game
Vscode configures the typera editor for MD
5. Oracle TABLESPACE
TCP's understanding of three handshakes and four waves
Vant weave swipecell sets multiple buttons
随机推荐
Record of problems in ollvm compilation
The route of wechat applet jumps again without triggering onload
Operator priority, one catch, no doubt
Find the combination number acwing 888 Find the combination number IV
Modnet matting model reproduction
Redis-01. First meet redis
2.Oracle-数据文件的添加及管理
Application of recyclerview
__ builtin_ Popcount() counts the number of 1s, which are commonly used in bit operations
中国剩余定理 AcWing 204. 表达整数的奇怪方式
[QT] QT multithreading development qthread
Design specification for mobile folding screen
求组合数 AcWing 887. 求组合数 III
VLAN experiment
Chinese remainder theorem acwing 204 Strange way of expressing integers
LSA Type Explanation - detailed explanation of lsa-2 (type II LSA network LSA) and lsa-3 (type III LSA network Summary LSA)
LSA Type Explanation - lsa-5 (type 5 LSA - autonomous system external LSA) and lsa-4 (type 4 LSA - ASBR summary LSA) explanation
Presentation of attribute value of an item
our solution
Simple selection sort of selection sort