英文:
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<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());
}
}
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 < Graph.graphSize; i++) {
for (int j = 0; j < Graph.graphSize; j++) {
A[i][j] = another.A[i][j];
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论