android中怎么调用String作为变量名;如下HashMap<String, String> xm = new HashMap<String, String>(); xm.put("nr1","fen1"); xm.put("nr2","fen2"); xm.put("nr3","fen3"); xm.put("nr4","fen4"); xm.put("nr5","fen5"); for (String k: xm.keySet()){ k = findViewById(R.id.k); }
时间: 2023-05-15 20:05:12 浏览: 53
在Java中,不能直接使用String作为变量名。但是,你可以使用HashMap来存储String类型的键值对,然后通过键来访问对应的值。在你的代码中,你已经使用了HashMap来存储键值对,可以通过以下方式来访问值:
String value = xm.get("nr1");
这将返回键为"nr1"的值"fen1"。如果你想要将值设置为变量,可以使用以下代码:
String k = xm.get("nr1");
这将把"fen1"赋值给变量k。在你的for循环中,你可以使用以下代码来访问键:
for (String k: xm.keySet()){
String value = xm.get(k);
// do something with key and value
}
这将遍历HashMap中的所有键,并使用每个键来访问对应的值。注意,你不能使用k = findViewById(R.id.k)来设置变量k的值,因为k是一个循环变量,它的值会在每次迭代中改变。
相关问题
HashMap<String, HashMap<String, Integer>>
HashMap<String, HashMap<String, Integer>>是一个嵌套的HashMap,其中外层HashMap的键是String类型,值是内层HashMap。内层HashMap的键也是String类型,值是Integer类型。可以通过以下代码示例创建和使用HashMap<String, HashMap<String, Integer>>:
```java
import java.util.HashMap;
public class Test {
public static void main(String[] args) {
HashMap<String, HashMap<String, Integer>> map = new HashMap<>();
// 添加数据
HashMap<String, Integer> innerMap1 = new HashMap<>();
innerMap1.put("语文", 90);
innerMap1.put("数学", 95);
map.put("张三", innerMap1);
HashMap<String, Integer> innerMap2 = new HashMap<>();
innerMap2.put("语文", 85);
innerMap2.put("数学", 92);
map.put("李四", innerMap2);
// 获取数据
System.out.println(map.get("张三").get("语文")); // 输出90
System.out.println(map.get("李四").get("数学")); // 输出92
// 删除数据
map.get("张三").remove("语文"); System.out.println(map.get("张三")); // 输出{数学=95}
}
}
```
string 转 HashSet<HashMap<String,Object>>
这个操作需要根据具体的需求来确定,因为一个string转换成一个HashSet<HashMap<String,Object>>可能会有不同的实现方式。以下是一种可能的实现方式:
```java
public HashSet<HashMap<String, Object>> stringToMapSet(String input) {
HashSet<HashMap<String, Object>> result = new HashSet<>();
try {
JSONArray jsonArray = new JSONArray(input);
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObj = jsonArray.getJSONObject(i);
HashMap<String, Object> map = new HashMap<>();
Iterator<String> keys = jsonObj.keys();
while (keys.hasNext()) {
String key = keys.next();
map.put(key, jsonObj.get(key));
}
result.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
```
这个实现方式假设输入的字符串是一个JSON数组,每个元素都是一个JSON对象,每个JSON对象包含一些键值对。这个方法首先将字符串解析成一个JSONArray对象,然后遍历数组中的每个元素,将它转换成一个HashMap<String, Object>对象,并添加到结果集合中。这个方法使用了Java标准库中的JSONObject和JSONArray类来解析JSON格式的字符串。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)