I’m very new to Java, so I’m sorry if this is a bit too stupid for you. So, I have a class called Construct that has an instance variable previousState. I have a setter in that class, with the signature setPreviousState.
Then, in an other class I set the previous State of a Construct object with this code :
ArrayList<Construct> sequence = new ArrayList<Construct>();
do {
Construct minimum = priorityQueue.delMin();
for (Construct neighbor : minimum.neighbors()) {
neighbor.setPreviousState(minimum);
priorityQueue.insert(neighbor);
}
System.out.println(minimum);
if (minimum.isGoalState()) {
// Construct sequence backwards and return it
sequence.add(minimum);
while(minimum.previousState() != null) {
sequence.add(0, minimum.previousState());
}
return sequence;
}
} while (true);
But while(minimum.previousState() != null) is an infinite loop because previousState() always references the same object. Why?
You never change the value of
minimumin this loop, and it seems that the value ofminimum.previousState()is also constant for each object [no side affects to methodpreviousState()], you might want to addminimum = minimum.previousState();to yourwhileloop: