在Java中把k = [Guo, Meng, Zhao, Yang, Zhang, Wang, Li, Sun, He]变为类型变量
时间: 2023-07-02 22:12:55 浏览: 91
在Java中,我们可以把k = [Guo, Meng, Zhao, Yang, Zhang, Wang, Li, Sun, He]变为类型变量的方式如下:
```java
List<String> k = Arrays.asList("Guo", "Meng", "Zhao", "Yang", "Zhang", "Wang", "Li", "Sun", "He");
```
上述代码中,我们使用了Java中的List类型来存储字符串类型的元素,同时使用Arrays.asList方法将字符串数组转换为List类型。在声明List类型时,我们使用了泛型来指定List中元素的类型为String。
相关问题
1、利用Map接口,定义两个班的成绩表: 1班 2班 Zhang 92 Meng 93 Wang 94 Sun 92 Li 93.5 Guo 95 Zhao 98 Yang 99 Sun 100 He 99.5 并用Map的形式打印输出两个班的Key-Value值;输出成绩为100的Map映射;
好的,根据您的要求,我来给您展示一下Java代码:
```java
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// 定义班级成绩表
Map<String, Double> class1 = new HashMap<>();
class1.put("Zhang", 92.0);
class1.put("Wang", 94.0);
class1.put("Li", 93.5);
class1.put("Zhao", 98.0);
class1.put("Sun", 100.0);
Map<String, Double> class2 = new HashMap<>();
class2.put("Meng", 93.0);
class2.put("Sun", 92.0);
class2.put("Guo", 95.0);
class2.put("Yang", 99.0);
class2.put("He", 99.5);
// 打印输出两个班的Key-Value值
System.out.println("1班成绩表:");
for (Map.Entry<String, Double> entry : class1.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
System.out.println("2班成绩表:");
for (Map.Entry<String, Double> entry : class2.entrySet()) {
System.out.println(entry.getKey() + " : " + entry.getValue());
}
// 输出成绩为100的Map映射
System.out.println("成绩为100的Map映射:");
for (Map.Entry<String, Double> entry : class1.entrySet()) {
if (entry.getValue() == 100.0) {
System.out.println("1班 " + entry.getKey() + " : " + entry.getValue());
}
}
for (Map.Entry<String, Double> entry : class2.entrySet()) {
if (entry.getValue() == 100.0) {
System.out.println("2班 " + entry.getKey() + " : " + entry.getValue());
}
}
}
}
```
输出结果为:
```
1班成绩表:
Zhang : 92.0
Wang : 94.0
Li : 93.5
Zhao : 98.0
Sun : 100.0
2班成绩表:
Meng : 93.0
Sun : 92.0
Guo : 95.0
Yang : 99.0
He : 99.5
成绩为100的Map映射:
1班 Sun : 100.0
```
1、利用Map接口,定义两个班的成绩表: 1班 2班 Zhang 92 Meng 93 Wang 94 Sun 92 Li 93.5 Guo 95 Zhao 98 Yang 99 Sun 100 He 99.5 并用Map的形式打印输出两个班的Key-Value值;输出成绩为100的Map映射;
下面是Java代码实现:
```java
import java.util.HashMap;
import java.util.Map;
public class ScoreTable {
public static void main(String[] args) {
Map<String, Double> class1 = new HashMap<>();
class1.put("Zhang", 92.0);
class1.put("Wang", 94.0);
class1.put("Li", 93.5);
class1.put("Zhao", 98.0);
class1.put("Sun", 100.0);
Map<String, Double> class2 = new HashMap<>();
class2.put("Meng", 93.0);
class2.put("Sun", 92.0);
class2.put("Guo", 95.0);
class2.put("Yang", 99.0);
class2.put("He", 99.5);
System.out.println("1班:");
for (Map.Entry<String, Double> entry : class1.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
System.out.println("2班:");
for (Map.Entry<String, Double> entry : class2.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
Map<String, Double> score100 = new HashMap<>();
for (Map.Entry<String, Double> entry : class1.entrySet()) {
if (entry.getValue() == 100.0) {
score100.put(entry.getKey(), entry.getValue());
}
}
for (Map.Entry<String, Double> entry : class2.entrySet()) {
if (entry.getValue() == 100.0) {
score100.put(entry.getKey(), entry.getValue());
}
}
System.out.println("成绩为100的Map映射:");
for (Map.Entry<String, Double> entry : score100.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
}
}
```
输出结果为:
```
1班:
Zhang: 92.0
Wang: 94.0
Li: 93.5
Zhao: 98.0
Sun: 100.0
2班:
Meng: 93.0
Sun: 92.0
Guo: 95.0
Yang: 99.0
He: 99.5
成绩为100的Map映射:
Sun: 100.0
```
阅读全文