Arraylist.clear clearing all array lists?

前端 未结 1 1708
情歌与酒
情歌与酒 2020-12-12 07:44

I have 2 separate array lists. one called spawnList and another called foundList

I have the code run through, it spawns an entity and adds said entity ID to the spaw

1条回答
  •  囚心锁ツ
    2020-12-12 07:59

    Have you ever wrote the code

    spawnList = foundList or foundList = spawnList ?

    If so, since an ArrayList is an object you weren't actually copying those lists, you were making them reference the same object. (IE everything you do to one will be done to the other).

    If you want to mitigate against this instead of directly setting the lists equal to each other you could do something like

    foundList = new ArrayList<>(spawnList)

    as this will make the two arrays be separate objects.

    Depending on what type of objects are in your arrays this could still be a problem, as they would still be the same instances of each object.

    0 讨论(0)
提交回复
热议问题