稀疏数组

 1 public class Array05 {
 2     public static void main(String[] args) {
 3 //-------------//创建一个稀疏数组------------------------------------------------------------------------------------------//
 4         int[][] arr01 = new int[11][11];
 5         arr01[1][2] = 1;
 6         arr01[2][3] = 2;
 7         //输出原始数组,方法1
 8         System.out.println("输出原始数组:");
 9         for (int i = 0; i < arr01.length; i++) {
10             for (int j = 0; j < arr01[i].length; j++){
11                 System.out.print(arr01[i][j] + "\t");
12             }
13             System.out.print("\n");
14         }
15         System.out.println("-------------------------------------------------");
16 //-------------//转换成稀疏数组进行保存-------------------------------------------------------------------------------------//
17             //获取一下有效值的个数
18         int sum = 0;
19         for (int i = 0; i < 11; i++) {
20             for (int j = 0; j < 11; j++) {
21                 if (arr01[i][j] != 0){
22                     sum++;
23                 }
24             }
25         }
26         System.out.println("有效值的个数:" + sum);
27             //输出稀疏矩阵
28         int[][] arr02 = new int[sum+1][3];
29         arr02[0][0] = 11;
30         arr02[0][1] = 11;
31         arr02[0][2] = sum;
32         int count = 0;
33         for (int i = 1; i < arr01.length; i++) {
34             for (int j = 0; j < arr01.length; j++) {
35                 if (arr01[i][j] !=  0){
36                     count++;
37                     arr02[i][0] = i;
38                     arr02[i][1] = j;
39                     arr02[i][2] = arr01[i][j];
40                 }
41             }
42         }
43         //输出原始数组,方法2
44         System.out.println("-------------------------------------------------");
45         for (int[] a: arr02){
46             for (int x: a){
47                 System.out.print(x +"\t");
48             }
49             System.out.print("\n");
50         }
51 //-------------//转换数组进行保存------------------------------------------------------------------------------------------//
52         int[][] arr03 = new int[arr02[0][0]][arr02[0][1]];
53         for (int i = 1; i < arr02.length; i++) {
54             arr03[arr02[i][0]][arr02[i][1]] = arr02[i][2];
55         }
56         System.out.println("-------------------------------------------------");
57         for (int[] a: arr03){
58             for (int x: a){
59                 System.out.print(x +"\t");
60             }
61             System.out.print("\n");
62         }
63     }
64 }

结果:

输出原始数组:
0    0    0    0    0    0    0    0    0    0    0    
0    0    1    0    0    0    0    0    0    0    0    
0    0    0    2    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
-------------------------------------------------
有效值的个数:2
-------------------------------------------------
11    11    2    
1    2    1    
2    3    2    
-------------------------------------------------
0    0    0    0    0    0    0    0    0    0    0    
0    0    1    0    0    0    0    0    0    0    0    
0    0    0    2    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    
0    0    0    0    0    0    0    0    0    0    0    

Process finished with exit code 0

 

上一篇:数组的简单应用


下一篇:黑苹果笔记 - nuc8i5BEH(无改机,含USB网卡,Clover引导)