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

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

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

问题

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

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

  1. public class Graph {
  2. int[][] A;
  3. public static final int graphSize = 5;
  4. public Graph() {
  5. A = new int[graphSize][graphSize];
  6. }
  7. public Graph(Graph another) {
  8. this.A = another.A;
  9. }
  10. //这是问题所在,其他部分都是为了测试而存在。
  11. public List<Graph> getAllPossibleGraphs(int playerTurn) {
  12. List<Graph> possibleGraphs = new ArrayList<>();
  13. for (int i = 0; i < graphSize; i++) {
  14. for (int j = 0; j < graphSize; j ++) {
  15. if (i != j && 0 == this.A[i][j]) {
  16. Graph newGraph = new Graph(this);
  17. newGraph.insertLine(i, j, playerTurn);
  18. possibleGraphs.add(newGraph);
  19. }
  20. }
  21. }
  22. return possibleGraphs;
  23. }
  24. public void insertLine(int node1, int node2, int player) {
  25. this.A[node1][node2] = player;
  26. this.A[node2][node1] = player;
  27. }
  28. public void printGraph() {
  29. for (int i = 0; i < Graph.graphSize; i++) {
  30. for (int j = 0; j < Graph.graphSize; j++) {
  31. System.out.print(this.A[i][j] + ", ");
  32. }
  33. System.out.println("");
  34. }
  35. }
  36. }
  1. public class Test {
  2. public static void main(String[] args) {
  3. Graph G = new Graph();
  4. G.insertLine(0, 1, 1);
  5. List<Graph> testList = G.getAllPossibleGraphs(2);
  6. testList.forEach(graph -> graph.printGraph());
  7. }
  8. }

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

  1. 0, 1, 2, 2, 2,
  2. 1, 0, 2, 2, 2,
  3. 2, 2, 0, 2, 2,
  4. 2, 2, 2, 0, 2,
  5. 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.

  1. public class Graph {
  2. int[][] A;
  3. public static final int graphSize = 5;
  4. public Graph() {
  5. A = new int[graphSize][graphSize];
  6. }
  7. public Graph(Graph another) {
  8. this.A = another.A;
  9. }
  10. //This is where the problem is, everything else is so it would run if tested.
  11. public List&lt;Graph&gt; getAllPossibleGraphs(int playerTurn) {
  12. List&lt;Graph&gt; possibleGraphs = new ArrayList&lt;&gt;();
  13. for (int i = 0; i &lt; graphSize; i++) {
  14. for (int j = 0; j &lt; graphSize; j ++) {
  15. if (i != j &amp;&amp; 0 == this.A[i][j]) {
  16. Graph newGraph = new Graph(this);
  17. newGraph.insertLine(i, j, playerTurn);
  18. possibleGraphs.add(newGraph);
  19. }
  20. }
  21. }
  22. return possibleGraphs;
  23. }
  24. public void insertLine(int node1, int node2, int player) {
  25. this.A[node1][node2] = player;
  26. this.A[node2][node1] = player;
  27. }
  28. public void printGraph() {
  29. for (int i = 0; i &lt; Graph.graphSize; i++) {
  30. for (int j = 0; j &lt; Graph.graphSize; j++) {
  31. System.out.print(this.A[i][j] + &quot;, &quot;);
  32. }
  33. System.out.println(&quot;&quot;);
  34. }
  35. }
  36. }
  1. public class Test {
  2. public static void main(String[] args) {
  3. Graph G = new Graph();
  4. G.insertLine(0, 1, 1);
  5. List&lt;Graph&gt; testList = G.getAllPossibleGraphs(2);
  6. testList.forEach(graph -&gt; graph.printGraph());
  7. }
  8. }

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

  1. 0, 1, 2, 2, 2,
  2. 1, 0, 2, 2, 2,
  3. 2, 2, 0, 2, 2,
  4. 2, 2, 2, 0, 2,
  5. 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,但可能需要一份复印件。诚然,我不理解逻辑(太热)。

  1. public Graph(Graph another) {
  2. this();
  3. for (int i = 0; i < Graph.graphSize; i++) {
  4. for (int j = 0; j < Graph.graphSize; j++) {
  5. A[i][j] = another.A[i][j];
  6. }
  7. }
  8. }
英文:

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

  1. public Graph(Graph another) {
  2. this();
  3. for (int i = 0; i &lt; Graph.graphSize; i++) {
  4. for (int j = 0; j &lt; Graph.graphSize; j++) {
  5. A[i][j] = another.A[i][j];
  6. }
  7. }
  8. }

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:

确定