I have the following code (this is just an example that I thought of for the concept I am having trouble with)
String fileName = "example3.txt";
Reader reader = new Reader();
ArrayList<String> vertices = reader.getVertices(fileName);
Matrix m = new Matrix(vertices);
Matrix m2 = m;
m.setEdges(fileName);
m2.print();
m.print();
what I want is m2 to be what m was before m gets changed because of the setEdges. But when I print both m and m2 they seem the same:
vertices are: [A, B, C, D, E, F, G, H, I]
matrix [[*, A, B, C, D, E, F, G, H, I], [A, 0, 1, 0, 0, 0, 0, 0, 0, 0], [B, 1, 0, 1, 0, 0, 0, 0, 0, 0], [C, 0, 1, 0, 1, 1, 1, 1, 0, 0], [D, 0, 0, 1, 0, 1, 0, 0, 0, 0], [E, 0, 0, 1, 1, 0, 1, 1, 0, 0], [F, 0, 0, 1, 0, 1, 0, 1, 0, 0], [G, 0, 0, 1, 0, 1, 1, 0, 0, 0], [H, 0, 0, 0, 0, 0, 0, 0, 0, 0], [I, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
matrix [[*, A, B, C, D, E, F, G, H, I], [A, 0, 1, 0, 0, 0, 0, 0, 0, 0], [B, 1, 0, 1, 0, 0, 0, 0, 0, 0], [C, 0, 1, 0, 1, 1, 1, 1, 0, 0], [D, 0, 0, 1, 0, 1, 0, 0, 0, 0], [E, 0, 0, 1, 1, 0, 1, 1, 0, 0], [F, 0, 0, 1, 0, 1, 0, 1, 0, 0], [G, 0, 0, 1, 0, 1, 1, 0, 0, 0], [H, 0, 0, 0, 0, 0, 0, 0, 0, 0], [I, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
Why? I said m2=m before m was changed and yet m2 is taking the change into account. What should be happening is that m2 should be :
matrix [[*, A, B, C, D, E, F, G, H, I], [A, 0, 0, 0, 0, 0, 0, 0, 0, 0], [B, 0, 0, 0, 0, 0, 0, 0, 0, 0], [C, 0, 0, 0, 0, 0, 0, 0, 0, 0], [D, 0, 0, 0, 0, 0, 0, 0, 0, 0], [E, 0, 0, 0, 0, 0, 0, 0, 0, 0], [F, 0, 0, 0, 0, 0, 0, 0, 0, 0], [G, 0, 0, 0, 0, 0, 0, 0, 0, 0], [H, 0, 0, 0, 0, 0, 0, 0, 0, 0], [I, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
can someone explain what is going on and how I can get the new object that I want a copy of to not take into account the changes made to the object it is a copy of? Is the clone method involved? But I don't have a .clone() appearing when I go m. so what should I do?
Thanks