简体   繁体   English

将list1添加到list2并更新list1而不影响list2

[英]Adding list1 to list2 and updating list1 without affecting list2

I'm working on a board game and currently trying to save all surrounding neighbors in a 2D array as lists in a list. 我正在做一个棋盘游戏,目前正在尝试将2D数组中的所有周围邻居保存为列表中的列表。 The problem that I'm having is that when I have saved list1 to list2, and then move on in the iteration and change list1 to something else, it also affects list2 so I lose the values of the other neighbor. 我遇到的问题是,当我将list1保存到list2,然后继续进行迭代并将list1更改为其他名称时,它也影响了list2,因此我失去了另一个邻居的值。

This is what I have tried: 这是我尝试过的:

     private ArrayList<ArrayList> forcedPlays = new ArrayList<ArrayList>();
     private ArrayList<Integer> forcedPlay = new ArrayList<Integer>();   

    private void findNeighbors(){
     for (int y = -1; y <=1 ; y+=2) {
           for (int x = -1; x <= 1; x+=2) {
               if (board[myY+y][myX+x]!=null){
                   enemyY = myY+y;
                   enemyX = myX+x;
                    forcedPlay.addAll(Arrays.asList(
                                     Integer.valueOf(enemyY),
                                     Integer.valueOf(enemyX)));

     forcedPlays.add(forcedPlay);
     forcedPlay.clear()}}}}

If my player is surrounded by two neighbors I would expect the output of the forcedPlays to look like for example: [[2,1],[4,3]], but instead it looks like [[],[]]. 如果我的玩家被两个邻居包围,那么我可以期望forcePlays的输出看起来像:[[2,1],[4,3]],但是看起来却像[[],[]]。 So what I what I don't understand is how do i add list1 to list2 and then cut the connection between them so when I clear list1 it wont clear list2? 所以我不明白的是如何将list1添加到list2,然后断开它们之间的连接,所以当我清除list1时,不会清除list2吗?

You can create a copy of forcedPlay before adding it. 您可以在添加之前创建一份forcedPlay副本。

forcedPlays.add(new ArrayList<Integer>(forcedPlay));

This way changes made to forcedPlay won't affect the list that was added to the outer list. 这样,对forcedPlay所做的更改将不会影响添加到外部列表的列表。

You're doing a reference copy. 您正在做参考副本。 Basically list1 is pointing at some of the elements of list2. 基本上list1指向list2的某些元素。 You need to add to list2 copies of elements in list1, clones. 您需要将list1克隆中的元素副本添加到list2。

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

相关问题 Java: list1=list2 // list1==list2? - Java: list1=list2 // list1==list2? java项目list1到list2 - java items list1 to list2 在不使用流的情况下过滤 list1 中不在 list2 java 中的元素 - Filter elements in list1 that are not in list2 java without using streams 我想从list1中删除list2元素并返回list1 - I want remove list2 elements from list1 and return the list1 如何合并清单 <Date> list1和List <Date> Java中的list2? - How to Union List<Date> list1 and List<Date> list2 in Java? 更新list1中的参数 <object> 来自list2 <object> 通过使用java 8 - Update param in list1<object> from list2<object> by using java 8 从 List1 中删除值也会从 List2 中删除值? - Removing values from List1 also removes values from List2? Java 8 Streams:计算元素的出现次数(列表<String> list1) 来自文本数据列表(List<String> 清单 2) - Java 8 Streams : Count the occurrence of elements(List<String> list1) from list of text data(List<String> list2) 检查list2.containsAll(list1)但字符串不完全相同 - check if list2.containsAll(list1) but with not exact same String 从LinkedList(list1)的实例创建了一个子列表(list3),在子列表上调用了clear()方法,也不希望list1也被更改 - created a sublist (list3) from an instance of LinkedList (list1) , invoked clear() method on sublist, did not expect list1 to be changed as well
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM