Java稀疏数组笔记

来自狂神说java课程
前往课程:点我进入

课程笔记:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
public class Demo5 {
//稀疏数组 狂神课程P59
public static void main(String[] args) {
//定义数组
int[][] a = new int[11][11];
a[1][2] = 1;
a[2][3] = 2;
//计算行列
int hang = 0;
int lie = 0;
int sum = 0;
//遍历数组。这里是foreach
System.out.println("数组的遍历");

System.out.println("使用foreach遍历");
System.out.println("=========================================");
for (int[] i : a) {
for (int j : i) {
System.out.print(j + "\t");
lie++;
}
System.out.println();
hang++;
}
lie = lie / hang;
System.out.println("=========================================");
//遍历数组。这里是for循环
System.out.println("使用for遍历");
System.out.println("=========================================");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.print(a[i][j] + "\t");
//System.out.print(a[i][j]+'\t');
}
System.out.println();
}
System.out.println("=========================================");
//计算有效值
for (int[] i : a) {
for (int j : i) {

if (j != 0)
sum++;

}
}
//创建稀疏数组
int[][] b = new int[sum + 1][3];
//第一行
b[0][0] = hang;
b[0][1] = lie;
b[0][2] = sum;
//赋值
int count = 1;
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
if (a[i][j] != 0) {
b[count][0] = i;
b[count][1] = j;
b[count][2] = a[i][j];
count++;
}
}
}

System.out.println("创建稀疏数组完成!");
System.out.println("稀疏数组:");
System.out.println("foreach遍历");
System.out.println("=========");
for (int[] i : b) {
for (int j : i) {
System.out.print(j + "\t");

}
System.out.println();

}
System.out.println("=========");
System.out.println("for遍历");
System.out.println("=========");
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
System.out.print(b[i][j] + "\t");
}
System.out.println();
}
System.out.println("=========");
System.out.println("将稀疏数组转为二维数组");
System.out.println("=========================================");
int[][] c = new int[b[0][0]][b[0][1]];
for (int i = 0; i < b.length; i++) {
if (i != 0) {
c[b[i][0]][b[i][1]] = b[i][2];
}
}
for (int[] e : c) {
for (int f : e) {
System.out.print(f + "\t");
}
System.out.println();
}
System.out.println("=========================================");
}
}

输出效果:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
数组的遍历
使用foreach遍历
=========================================
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
=========================================
使用for遍历
=========================================
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
=========================================
创建稀疏数组完成!
稀疏数组:
foreach遍历
=========
11 11 2
1 2 1
2 3 2
=========
for遍历
=========
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
=========================================

进程已结束,退出代码为 0