的ArrayList >为String []
问题描述:
我在的ArrayList <HashMap中<字符串,字符串>>为String []
ArrayList<HashMap<String,String>>
从我的web服务获取的数据现在我想把上述的每个对象转换为
String[]
如何我是否这样做? 任何帮助将不胜感激!
答
尝试
ArrayList<HashMap<String, String>> test = new ArrayList<HashMap<String, String>>();
HashMap<String, String> n = new HashMap<String, String>();
n.put("a", "a");
n.put("b", "b");
test.add(n);
HashMap<String, String> m = test.get(0);//it will get the first HashMap Stored in array list
String strArr[] = new String[m.size()];
int i = 0;
for (HashMap<String, String> hash : test) {
for (String current : hash.values()) {
strArr[i] = current;
i++;
}
}
答
HashMap的用途应该是HashValues的索引,以便更快地找到值。我不知道为什么你有重点和值作为字符串,但如果你只需要值,你可以做这样的:
ArrayList<HashMap<String, String>> test = new ArrayList<>();
String sum = "";
for (HashMap<String, String> hash : test) {
for (String current : hash.values()) {
sum = sum + current + "<#>";
}
}
String[] arr = sum.split("<#>");
这不是一个很好的方式,但要求不是太;)
+0
谢谢你它的工作:) –
答
ArrayList<HashMap<String, String>> meterList = controller.getMeter();
HashMap<String, String> mtiti = meterList.get(0);//it will get the first HashMap Stored in array list
String[] strMeter = new String[mtiti.size()];
String meter = "";
for (HashMap<String, String> hash : meterList) {
for (String current : hash.values()) {
meter = meter + current + "<#>";
}
}
String[] arr = meter.split("<#>");
它包含了从服务JSON数据web服务获得的数据。我需要将每个字段提取为一个字符串数组,以将它们填充到viwepagers和列表视图等不同视图中。 –
http://*.com/questions/1090556/java-how-to-convert-hashmapstring-object-to-array.check这可能有所帮助。 – Raghunandan
在数组中,你想放置hashmap的键,值或两者? – Alberto