简体   繁体   English

在给定的for和while循环中使用哪种数据结构代替Arraylist?

[英]Which data structure to use instead of Arraylist in the given for and while loop?

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/Country size: 0

The test method logs 0 because I have cleared the citiesList after I have put the citiesList in a HashMap. 测试方法记录为0,因为我已经清除了citiesList我已经把citiesList一个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 ? 如果我删除citysList.clear()行,那么测试方法将记录为9。 是否可以通过一种方法将测试方法返回3(而不是0或9)? What should I use instead of ArrayList to achieve the desired result? 我应该使用什么代替ArrayList来达到期望的结果?

The easiest solution is to create a new ArrayList , that way it will be a different reference; 最简单的解决方案是创建一个新的ArrayList ,这样它将是一个不同的引用。 and the behavior will be as your probably expected. 行为将与您预期的一样。 Change 更改

citiesList.clear();

to

citiesList = new ArrayList<>();

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<>();

When you do countryCities.put(key, citiesList); 当您执行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. 如果您有许多城市要关注内存使用情况,则还需要实现某种缓存机制,以便在任何给定时刻仅将一部分城市加载到内存中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM