-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPrintMatrix.java
More file actions
54 lines (46 loc) · 2 KB
/
Copy pathPrintMatrix.java
File metadata and controls
54 lines (46 loc) · 2 KB
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
package heyingzhe;
import java.util.HashMap;
import java.util.Map;
public class PrintMatrix {
public void printMatrix(double[][] matrix, int length) {
for (int i = 0; i < Math.min(length, matrix.length); i++) {
for (int j = 0; j < Math.min(length, matrix[i].length); j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
public void printMatrix(int[][] matrix, int length) {
for (int i = 0; i < Math.min(length, matrix.length); i++) {
for (int j = 0; j < Math.min(length, matrix[i].length); j++) {
System.out.print(matrix[i][j] + "\t");
}
System.out.println();
}
}
// 方法:统计大于阈值的元素及其出现次数
public void countValuesAboveThreshold(double[][] array, double threshold) {
// 使用一个 HashMap 来存储每个大于阈值的值及其出现的次数
Map<Double, Integer> valueCountMap = new HashMap<>();
// 遍历二维数组
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
double value = array[i][j];
// 如果元素大于阈值
if (value > threshold) {
// 如果 Map 中已存在该值,则增加其计数
valueCountMap.put(value, valueCountMap.getOrDefault(value, 0) + 1);
}
}
}
// 打印出大于阈值的每个值及其出现的次数
if (valueCountMap.isEmpty()) {
System.out.println("没有值大于阈值 " + threshold);
} else {
System.out.println("大于阈值 " + threshold + " 的值及其出现次数:");
for (Map.Entry<Double, Integer> entry : valueCountMap.entrySet()) {
System.out.println("值: " + entry.getKey() + ", 次数: " + entry.getValue());
}
}
}
}