JSONObject jObject = new JSONObject(jsonString);
Iterator<String> keys = jObject.keys();
String key;
JSONArray jsonArray;
String value;
while (keys.hasNext()) {
key = keys.next();
countriesList.add(key);
jsonArray = jObject.getJSONArray(key);
for (int i = 0; i < jsonArray.length(); i++) {
value = jsonArray.getString(i);
citiesList.add(value);
}
countryCities.put(key, citiesList);
citiesList.clear();
Assume the following JSON file:
假设以下JSON文件:
{
"China": [
"Guangzhou",
"Fuzhou",
"Beijing"
],
"Nepal": [
"Pokhara",
"Kathmandu",
"Hetauda"
],
"India": [
"Delhi",
"Mumbai",
"Chennai"
]
}
There is a test method :
有一种测试方法:
private void CountriesListIterator(){
ArrayList<String> country= countryCities.get("China");
Log.d("Country size", String.valueOf(country.size()));
}
The test method when called from onCreate() returns :06-18 09:05:37.245 17603-17603/com.example.user.statusok D/Country size: 0
从onCreate()调用时的测试方法返回:06-18 09:05:37.245 17603-17603 / com.example.user.statusok D /国家/地区大小:0
The test method logs 0 because I have cleared the citiesList after I have put the citiesList in a HashMap. If I remove the citiesList.clear() line, the test method logs 9. Is there a way in which if I run the test method it returns 3 instead of 0 or 9 ? What should I use instead of ArrayList to achieve the desired result?
测试方法记录0,因为我已将citiesList放入HashMap后清除了citiesList。如果我删除citiesList.clear()行,测试方法会记录9.有没有办法在运行测试方法时返回3而不是0或9?我应该使用什么而不是ArrayList来实现所需的结果?
3 个解决方案
#1
0
You should not clear. You're getting zero because you are.
你不应该清楚。因为你是零,所以你得到零。
When you don't, you're getting 9 because there's only ever one list you add to.
当你不这样做时,你得到9,因为你只添加了一个列表。
Both are a symptom of holding onto the same object reference.
两者都是保持相同对象引用的症状。
You need to make a new ArrayList for every country
您需要为每个国家/地区创建一个新的ArrayList
while (keys.hasNext()) {
List<String> cities = new ArrayList<>();
#2
1
The easiest solution is to create a new ArrayList
, that way it will be a different reference; and the behavior will be as your probably expected. Change
最简单的解决方案是创建一个新的ArrayList,这将是一个不同的引用;并且行为将如你所料。更改
citiesList.clear();
to
citiesList = new ArrayList<>();
#3
0
When you do countryCities.put(key, citiesList);
, you add a reference to the list to your map. Since you reuse the same list, each reference points to the exact same list object. Instead, you need to create a new list of cities to add to the map. If you have many cities such that memory usage becomes a concern, you will also need to implement some kind of caching mechanism so that only a subset of your cities are loaded into memory at any given moment.
当您执行countryCities.put(key,citiesList);时,您将向列表添加对列表的引用。由于您重复使用相同的列表,因此每个引用都指向完全相同的列表对象。相反,您需要创建要添加到地图的新城市列表。如果你有许多城市,内存使用成为一个问题,你还需要实现某种缓存机制,以便在任何给定时刻只有一部分城市被加载到内存中。
#1
0
You should not clear. You're getting zero because you are.
你不应该清楚。因为你是零,所以你得到零。
When you don't, you're getting 9 because there's only ever one list you add to.
当你不这样做时,你得到9,因为你只添加了一个列表。
Both are a symptom of holding onto the same object reference.
两者都是保持相同对象引用的症状。
You need to make a new ArrayList for every country
您需要为每个国家/地区创建一个新的ArrayList
while (keys.hasNext()) {
List<String> cities = new ArrayList<>();
#2
1
The easiest solution is to create a new ArrayList
, that way it will be a different reference; and the behavior will be as your probably expected. Change
最简单的解决方案是创建一个新的ArrayList,这将是一个不同的引用;并且行为将如你所料。更改
citiesList.clear();
to
citiesList = new ArrayList<>();
#3
0
When you do countryCities.put(key, citiesList);
, you add a reference to the list to your map. Since you reuse the same list, each reference points to the exact same list object. Instead, you need to create a new list of cities to add to the map. If you have many cities such that memory usage becomes a concern, you will also need to implement some kind of caching mechanism so that only a subset of your cities are loaded into memory at any given moment.
当您执行countryCities.put(key,citiesList);时,您将向列表添加对列表的引用。由于您重复使用相同的列表,因此每个引用都指向完全相同的列表对象。相反,您需要创建要添加到地图的新城市列表。如果你有许多城市,内存使用成为一个问题,你还需要实现某种缓存机制,以便在任何给定时刻只有一部分城市被加载到内存中。