I am trying to print the content of a PriorityQueue to an output file by converting it to an ArrayList.
(NOTE: I'm not printing to the console!)
Besides I declared a toString method, I am still getting the outputs like this: I@7c3df479
Converting the queue to ArrayList:
private PriorityQueue<GameState> unexpanded = new PriorityQueue<>(Comparator.comparing(GameState::getF_n));
...
public ArrayList<GameState> getUnexpanded()
{
ArrayList<GameState> unExpanded = new ArrayList<>(unexpanded);
return unExpanded;
}
Getting the ArrayList and trying it to print:
private void printSolution() throws IOException
{
FileWriter outFile = new FileWriter("output.txt");
PrintWriter output = new PrintWriter(outFile);
ArrayList<GameState> unexpanded = game.getUnexpanded();
for (int i = 0; i < unexpanded.size(); i++)
{
output.printf(unexpanded.get(i).toString() + "\n");
}
output.close();
}
toString method:
public class GameState
{
private int[][] grid;
...
@Override
public String toString()
{
return "{" + grid + "}";
}
}
Everything is working fine but the program print the contents like: I@7c3df479
Can anybody please help me with this?
Many thanks for the answers and comments in advance.