【问题标题】:need to save all the values coming in for loop?需要保存所有进入 for 循环的值?
【发布时间】:2016-07-26 03:47:37
【问题描述】:
if (Username.equals(RegisteredUSer)) {
    JSONArray nameObject = msg.getJSONArray("NameList");

    for (int j = 0; j < nameObject.length(); j++) {
        JSONObject name = nameObject.getJSONObject(j);
        USername        = name.getString("FirstName");
        USerweight      = name.getString("Weight");
        Weight          = Double.parseDouble(USerweight);
    }
}

这里我需要保存for循环中的所有用户名和权重,并将用户权重转换为双精度,并在同一类中提取保存值??

【问题讨论】:

  • 您发表了声明,但您的问题是什么?
  • 我知道你现在可能无法选择答案,但如果有效,请在时间限制结束后选择它。非常感谢,欢迎来到 Stackoverflow。

标签: java android arrays json


【解决方案1】:

你的版本:

ArrayList<String> username = new ArrayList<String>(); 
ArrayList<Double> weight = new ArrayList<Double>(); 

if (Username.equals(RegisteredUSer)) {
    JSONArray nameObject = msg.getJSONArray("NameList");

    for (int j = 0; j < nameObject.length(); j++) {
        JSONObject name = nameObject.getJSONObject(j);
        USername        = name.getString("FirstName");
        USerweight      = name.getString("Weight");
        Weight          = Double.parseDouble(USerweight);
        username.add(USername);
        weight.add(Weight);
    }
}

【讨论】:

    【解决方案2】:

    U可以使用字符串数组或arraylist。语法为ArrayList username=new ArrayList ();用户名.add(变量名);提取值调用 username.get (loopvariable);

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 2021-02-25
      • 2011-09-30
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多