Adding multiple objects to ArrayList using loop, all objects get updated when one is changed

huangapple go评论151阅读模式
英文:

Adding multiple objects to ArrayList using loop, all objects get updated when one is changed

问题

我一直在使用for循环来创建并将新的Graph对象添加到ArrayList中,以便在我的代码中其他地方使用,但当我打印列表时,里面的所有Graph对象都是相同的。

对一个对象的编辑会反映在其他对象上。当我使用调试器来检查发生了什么时,每个newGraph都有不同的ID,所以我不知道为什么会发生这种情况。以下是代码。我已经包含了足够的内容以便进行测试。

public class Graph {
    int[][] A;
    public static final int graphSize = 5;
  
    public Graph() {
        A = new int[graphSize][graphSize];
    }
    public Graph(Graph another) {
        this.A = another.A;
    }

    //这是问题所在,其他部分都是为了测试而存在。
    public List<Graph> getAllPossibleGraphs(int playerTurn) {
        List<Graph> possibleGraphs = new ArrayList<>();
        for (int i = 0; i < graphSize; i++) {
            for (int j = 0; j < graphSize; j ++) {
                if (i != j && 0 == this.A[i][j]) {
                    Graph newGraph = new Graph(this);
                    newGraph.insertLine(i, j, playerTurn);
                    possibleGraphs.add(newGraph);
                }
            }
        }
        return possibleGraphs;
    }

    public void insertLine(int node1, int node2, int player) {
        this.A[node1][node2] = player;
        this.A[node2][node1] = player;
    }
    public void printGraph() {
        for (int i = 0; i < Graph.graphSize; i++) {
            for (int j = 0; j < Graph.graphSize; j++) {
                System.out.print(this.A[i][j] + ", ");
            }
            System.out.println("");
        }
    }
}
public class Test {
    public static void main(String[] args) {
        Graph G = new Graph();
        G.insertLine(0, 1, 1);
        List<Graph> testList = G.getAllPossibleGraphs(2);
        testList.forEach(graph -> graph.printGraph());
    }
}

当我打印出列表时,我得到了以下所有Graphs:

0, 1, 2, 2, 2, 
1, 0, 2, 2, 2, 
2, 2, 0, 2, 2, 
2, 2, 2, 0, 2, 
2, 2, 2, 2, 0, 

如果需要任何帮助或建议,都将不胜感激,因为我已经尝试了一个多星期来解决这个问题,它让我发疯。

英文:

I have been using a for loop to create and add new Graph objects to an ArrayList for use elsewhere in my code, but when I print the list all of the Graph objects inside are identical.

An edit to one of the objects follows through on the rest. When I use the debugger to check what is going on each newGraph has a different ID so I have no idea why this is occurring. Code is below. I have included enough so that it is testable.

public class Graph {
int[][] A;
public static final int graphSize = 5;
public Graph() {
A = new int[graphSize][graphSize];
}
public Graph(Graph another) {
this.A = another.A;
}
//This is where the problem is, everything else is so it would run if tested.
public List&lt;Graph&gt; getAllPossibleGraphs(int playerTurn) {
List&lt;Graph&gt; possibleGraphs = new ArrayList&lt;&gt;();
for (int i = 0; i &lt; graphSize; i++) {
for (int j = 0; j &lt; graphSize; j ++) {
if (i != j &amp;&amp; 0 == this.A[i][j]) {
Graph newGraph = new Graph(this);
newGraph.insertLine(i, j, playerTurn);
possibleGraphs.add(newGraph);
}
}
}
return possibleGraphs;
}
public void insertLine(int node1, int node2, int player) {
this.A[node1][node2] = player;
this.A[node2][node1] = player;
}
public void printGraph() {
for (int i = 0; i &lt; Graph.graphSize; i++) {
for (int j = 0; j &lt; Graph.graphSize; j++) {
System.out.print(this.A[i][j] + &quot;, &quot;);
}
System.out.println(&quot;&quot;);
}
}
}
public class Test {
public static void main(String[] args) {
Graph G = new Graph();
G.insertLine(0, 1, 1);
List&lt;Graph&gt; testList = G.getAllPossibleGraphs(2);
testList.forEach(graph -&gt; graph.printGraph());
}
}

So when I print out the list I get all of the Graphs as follows:

0, 1, 2, 2, 2, 
1, 0, 2, 2, 2, 
2, 2, 0, 2, 2, 
2, 2, 2, 0, 2, 
2, 2, 2, 2, 0, 

Any help or advice would be appreciated as I have been trying to get a solution to this for over a week and it's driving me insane.

答案1

得分: 0

你正在分享A,但可能需要一份复印件。诚然,我不理解逻辑(太热)。

public Graph(Graph another) {
    this();
    for (int i = 0; i < Graph.graphSize; i++) {
        for (int j = 0; j < Graph.graphSize; j++) {
            A[i][j] = another.A[i][j];
        }
    }
}
英文:

You are sharing A, but probably need a copy. Admittedly I do not understand the logic (too hot).

public Graph(Graph another) {
this();
for (int i = 0; i &lt; Graph.graphSize; i++) {
for (int j = 0; j &lt; Graph.graphSize; j++) {
A[i][j] = another.A[i][j];
}
}
}

huangapple
  • 本文由 发表于 2020年8月12日 20:52:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/63376948.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定