Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Hashmap值在执行期间发生更改_Java_Android_Hashmap - Fatal编程技术网

Java Hashmap值在执行期间发生更改

Java Hashmap值在执行期间发生更改,java,android,hashmap,Java,Android,Hashmap,我在代码中使用Hashmap来保存一些数据。我正在使用以下代码 HashMap<Integer,ArrayList<Friends>> friendsHashMap = new HashMap<Integer, ArrayList<Friends>>(); ... ... ArrayList<Friends> friendsArrayList = new ArrayList<Friends>(); JSONArray jso

我在代码中使用Hashmap来保存一些数据。我正在使用以下代码

HashMap<Integer,ArrayList<Friends>> friendsHashMap = new HashMap<Integer, ArrayList<Friends>>();
...
...
ArrayList<Friends> friendsArrayList = new ArrayList<Friends>();
JSONArray jsonArray = (JSONArray) response.get("friends");
for (int i=0; i<jsonArray.length(); i++){
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        Friend friend = new Friend(jsonObject);
        friendsArrayList.add(friend);
       }

friendsHashMap.put(id,friendsArrayList); //Adding data to hash map
Log.d("friendsHashMap",friendsHashMap.toString());
Log.d("friendsArrayList ",friendsArrayList.size()+""); //Till this line previously loaded data remain normal
friendsLocationMapFragment.drawMap(friendsArrayList); //Once this line is executed the previously added data in the hashmap value changes to empty array.
HashMap-friendsHashMap=newhashmap();
...
...
ArrayList friendsArrayList=新建ArrayList();
JSONArray JSONArray=(JSONArray)response.get(“朋友”);
for(int i=0;i可能是
drawMap(friendsArrayList)
;方法将数据放入friendsHashMap,其Id与您之前在此行中放入数据的Id相同
friendsHashMap.put(Id,friendsArrayList);//将数据添加到哈希映射

若键相同,则HashMap将用新值覆盖以前的值


如果您确定键值在hashmap中是唯一的,那么有一种可能性,在drawMap(friendsArrayList)中您可能正在清除friendsArrayList或删除元素,或将新的空列表分配给friendsArrayList,因为您将相同的列表引用传递给drawMap

您在哪里定义
id
作为hashmap的键?它是一个全局值。
id
是一个整数,在执行这些代码行之前它会发生变化。您确定吗关于这一点?是的。添加到hashmap中的值在点上没有问题。先前添加的一个键的值更改为emty数组。drawMap对传递给它的数组做了什么?@Rani may你正在清除drawMap中的friendsArrayList,因为你正在将相同的列表引用传递给它drawMap函数在一个片段中。我有另一个数组列表,我将这个数组列表分配给它。每次我调用这个函数,我都会在分配这个之前清除现有的数组列表。这就是为什么你会得到空列表。100%确定。我想你读过我更新的答案了