当前位置:网站首页>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
 Insert picture description here
Convert to sparse array
 Insert picture description here
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	
原网站

版权声明
本文为[Said it was all]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140603261511.html