如何将数组列表转换为数组?
How to convert arraylist to array?
我有一个包含 JSON 个值的数组列表。我想把它转换成一个数组,然后我将它发送给MapActivity。我试了很多方法都失败了
for (int i = 0; i < matchFixture.length(); i++)
{
JSONObject c = matchFixture.getJSONObject(i);
String matchId = c.getString(TAG_MATCHID);
Log.d("matchId", matchId);
// hashmap for single match
HashMap<String, String> matchFixture = new HashMap<String, String>();
// adding each child node to HashMap key => value
matchFixture.put(TAG_MATCHID, matchId);
matchFixtureList.add(matchFixture);
//item=matchFixtureList.get(0).toString();
String[] stockArr = matchFixtureList.toArray(new String[matchFixtureList.size()]);
stockArr = matchFixtureList.toArray(stockArr);
for(String s : stockArr)
System.out.println(s);
}
更改以下行
String[] stockArr = matchFixtureList.toArray(new String[matchFixtureList.size()]);
stockArr = matchFixtureList.toArray(stockArr);
到
String[] stockArr = new String[matchFixtureList.size()];
stockArr = matchFixtureList.toArray(stockArr);
错误是您在数组初始化期间转换为数组,而这根本不需要。
您正在尝试将 HashMap
类型转换为 String
您的地图是 -
// hashmap for single match
HashMap<String, String> matchFixture = new HashMap<String, String>();
// adding each child node to HashMap key => value
matchFixture.put(TAG_MATCHID, matchId);
并且您正在将 matchFixture
(这是一个映射)添加到 ArrayList
(应该是 HashMap 列表)
matchFixtureList.add(matchFixture);
现在,如果你想将 matchFixtureList
转换为数组,那么你应该这样做 -
HashMap<String,String>[] stockArr = new HashMap[matchFixtureList.size()];
stockArr = matchFixtureList.toArray(stockArr);
for(HashMap<String,String> map : stockArr)
for(Map.Entry<String,String> entry : map.entrySet()){
Log.d("debug", entry.getKey() + ","+ entry.getValue());
}
我有一个包含 JSON 个值的数组列表。我想把它转换成一个数组,然后我将它发送给MapActivity。我试了很多方法都失败了
for (int i = 0; i < matchFixture.length(); i++)
{
JSONObject c = matchFixture.getJSONObject(i);
String matchId = c.getString(TAG_MATCHID);
Log.d("matchId", matchId);
// hashmap for single match
HashMap<String, String> matchFixture = new HashMap<String, String>();
// adding each child node to HashMap key => value
matchFixture.put(TAG_MATCHID, matchId);
matchFixtureList.add(matchFixture);
//item=matchFixtureList.get(0).toString();
String[] stockArr = matchFixtureList.toArray(new String[matchFixtureList.size()]);
stockArr = matchFixtureList.toArray(stockArr);
for(String s : stockArr)
System.out.println(s);
}
更改以下行
String[] stockArr = matchFixtureList.toArray(new String[matchFixtureList.size()]);
stockArr = matchFixtureList.toArray(stockArr);
到
String[] stockArr = new String[matchFixtureList.size()];
stockArr = matchFixtureList.toArray(stockArr);
错误是您在数组初始化期间转换为数组,而这根本不需要。
您正在尝试将 HashMap
类型转换为 String
您的地图是 -
// hashmap for single match
HashMap<String, String> matchFixture = new HashMap<String, String>();
// adding each child node to HashMap key => value
matchFixture.put(TAG_MATCHID, matchId);
并且您正在将 matchFixture
(这是一个映射)添加到 ArrayList
(应该是 HashMap 列表)
matchFixtureList.add(matchFixture);
现在,如果你想将 matchFixtureList
转换为数组,那么你应该这样做 -
HashMap<String,String>[] stockArr = new HashMap[matchFixtureList.size()];
stockArr = matchFixtureList.toArray(stockArr);
for(HashMap<String,String> map : stockArr)
for(Map.Entry<String,String> entry : map.entrySet()){
Log.d("debug", entry.getKey() + ","+ entry.getValue());
}