【问题标题】:Add content of Arraylist Hashmap in an integer array在整数数组中添加 Arraylist Hashmap 的内容
【发布时间】:2014-05-21 20:47:16
【问题描述】:

我需要一个帮助。我在用 ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>(); 存储时间和温度值。现在我需要将所有温度值复制到一个整数数组int[] temperature 中。

具体来说。我使用 Arraylist 将 json 中的值存储为,

// Decleration as
  ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String,String>>();
  JSONArray data = null;


data = json.getJSONArray("value");
for(int i = 0; i < data.length(); i++){
JSONObject c = data.getJSONObject(i);
String tem = c.getString("temperature");
String time = c.getString("time");
// Adding value HashMap key => value
   HashMap<String, String> map = new HashMap<String, String>();
   map.put("tim", tem);
   map.put("time", time);

   list.add(map);
}

现在我需要将温度值和时间存储在两个不同的数组中。我希望它们存储在下面

要存储在int[] temperature中的温度

时间存储在int[] time

我该怎么做?

谢谢

【问题讨论】:

  • 你为什么将时间和时间存储为字符串?将它们 JSON 解码为字符串只是为了必须将它们解析回数字似乎有问题。
  • 其实我是在使用这些值在 achartengine 上绘制图表。
  • 不不,不要在你的 JSONObject 上使用 getString(),首先使用 getInt()!!
  • 因为我在我的 Json 中发送像 Nov 12 4:35 这样的日期,所以我不能使用 int 因此我的第一个问题结构是我所拥有的。
  • 这对约会很有意义!对于温度,如果可能的话,我会使用 getInt() 。对于您想要使用 getString() 的日期,然后使用适当的函数将其转换为时间戳。但我会在你的 JSON 解析循环中这样做,所以你最终得到一个 HashMap 或其他东西而不是 HashMap

标签: arrays arraylist hashmap


【解决方案1】:
`/*Fill the recorded temperature*/
    int[] temperature = new int[count];
    for(int j = 0; j<count ;j++){
        int a = Integer.parseInt(list.get(j).get("tim"));
        temperature[j] = a;//Log.v("Tep", "val"+a);
    }`

这就是我所做的,它解决了我的问题。 我只是循环列表并获取索引元素。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-03
    • 2011-06-07
    • 2017-01-16
    • 2014-03-23
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    相关资源
    最近更新 更多