【问题标题】:ArrayList<HashMap<String,String>> to String[]ArrayList<HashMap<String,String>> 到 String[]
【发布时间】:2013-07-29 04:05:08
【问题描述】:

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

ArrayList<HashMap<String,String>>

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

String[]

我该怎么做? 任何帮助将不胜感激!

【问题讨论】:

  • 它包含从提供 json 数据的 web 服务获得的数据。我需要将每个字段提取为字符串数组,以便将它们填充到不同的视图中,例如 viwepagers 和列表视图。
  • stackoverflow.com/questions/1090556/… 这可能会有所帮助。
  • 你想在数组中放置 hashmap 的键、值还是两者都有?
  • 请覆盖我关于HashMap、ArrayList和其他Java集合类型的内部生活的教程here

标签: java android string arraylist hashmap


【解决方案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("<#>");

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

【讨论】:

    【解决方案2】:

    试试

    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++;
        }
    }
    

    【讨论】:

      【解决方案3】:
       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("<#>");
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-08-30
        • 1970-01-01
        • 2013-05-27
        • 1970-01-01
        • 2016-06-14
        相关资源
        最近更新 更多