So I have this generic object State which apparently changes its value despite not being edited. Here is what I mean - by invoking the same element in the Queue I am getting different outcomes.
System.out.println(mapString(que.element().getMapState()));
System.out.println(que.element());
The outcome:
The second outcome is the correct one.
Missing mapString method just prints the table char[][]:
private static String mapString(char[][] map) {
String mapString = "";
for(int k = 0; k<map.length; k++) {
for(int j = 0; j<map[k].length; j++) {
mapString += map[k][j];
}
mapString += "\n";
}
return mapString;
}
While getMapState just returns private variable from the State class
public char[][] getMapState() {
return map;
}
The Queue is the type of Queue<State> where State is my generic type. I can print it by the toString() method which results in the second (proper) outcome.
public String toString() {
return "Actual state:\n" + mapString;
}
Where the mapString is a String variable which is initiated in the State class constructor with the same exact String mapString(char[][] map) method.
public State(PlayerAddress player, HashMap<String, BoxAddress> boxList, char[][] map, String solution, String stateHash) {
this.player = player;
this.boxList = boxList;
this.map = map;
this.solution = solution;
this.stateHash = stateHash;
this.boxListString = boxListToString(boxList);
this.mapString = mapString(map);
}
However, later in the code I am not editing the object in the Queue but nevertheless it edits itself. What could be the reason? I can provide full code if that would change anything.
