5

我从我的网络服务中获取了数据

ArrayList<HashMap<String,String>>

现在我想将上面的每个对象转换为

String[]

我该怎么做呢?任何帮助将非常感激!

4

3 回答 3

8

尝试

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++;
    }
}
于 2013-07-29T11:59:13.260 回答
1

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("<#>");

这不是一个好方法,但请求也不是;)

于 2013-07-29T11:37:49.873 回答
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("<#>");
于 2016-01-27T08:12:36.517 回答