Sorry if this is a duplicate, but been struggling with other solutions. I'm making a pathfinding application for a project.
I wish to sort my arraylist of Edges that allows me to have import/export. The export doesn't sort them the way the import expects so technically works, but throws an error.
First column is start node, 2nd column is end node and 3rd column is irrelevant but is distance. I need to sort by the first and 2nd nodes to have them in order.
Current output:
6 8 5.0
7 8 1.0
7 9 5.0
3 5 3.0
3 4 1.0
8 9 1.0
1 2 5.0
1 3 2.0
1 4 4.0
4 9 10.0
4 5 1.0
4 7 6.0
2 3 4.0
2 6 3.0
5 7 6.0
Wanted Output:
1 2 5.0
1 3 2.0
1 4 4.0
2 3 4.0
2 6 3.0
...
My export file code, just trying to add Comparable to it, but was having issues.
public String exportToFile(String file) throws IOException {
FileWriter fileWriter;
ArrayList<Edge> edgeArrayList = new ArrayList<Edge>();
dijkstra.copyEdge(edgeArrayList);
String space = " ", fileExported = "File Exported";
try {
fileWriter = new FileWriter(file);
} catch (IOException e) {
return "File Not Found";
}
fileWriter.write(nodes.size() + "" + '\n');
for (Node node : nodes) {
fileWriter.write(node.name + space + node.x + space + node.y + '\n');
}
fileWriter.write(edgeArrayList.size() + "" + '\n');
for (Edge edge : edgeArrayList) {
fileWriter.write(edge.sourceNode.name + space + edge.destinationNode.name + space + edge.nodeWeight + '\n');
}
fileWriter.close();
return fileExported;
}
Thanks
Edit - Added Edge class
import javafx.beans.property.DoubleProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.scene.shape.Line;
import javafx.scene.text.Text;
import java.util.Comparator;
public class Edge {
public Node sourceNode;
public Node destinationNode;
public double nodeWeight;
public int roadType;
private final Line line;
private final Text text;
public Edge(Node source, Node destination, double weight) {
line = new Line();
text = new Text();
sourceNode = source;
destinationNode = destination;
nodeWeight = weight;
DoubleProperty startX = new SimpleDoubleProperty(source.x);
DoubleProperty startY = new SimpleDoubleProperty(source.y);
DoubleProperty endX = new SimpleDoubleProperty(destination.x);
DoubleProperty endY = new SimpleDoubleProperty(destination.y);
line.startXProperty().bind(startX);
line.startYProperty().bind(startY);
line.endXProperty().bind(endX);
line.endYProperty().bind(endY);
startX.bind(source.getCircle().centerXProperty());
startY.bind(source.getCircle().centerYProperty());
endX.bind(destination.getCircle().centerXProperty());
endY.bind(destination.getCircle().centerYProperty());
text.setText(weight + "");
text.xProperty().bind((startX.add(endX)).divide(2));
text.yProperty().bind((startY.add(endY)).divide(2));
}
public Line getLine() {
return line;
}
public Text getText() {
return text;
}
public String toString() {
return String.format("(%s -> %s,%f)", sourceNode.name, destinationNode.name, nodeWeight);
}
}
Edit#2 - added Node class
public class Node {
public double x, y;
public String name;
public boolean isVisited;
public Circle circle;
public Text text;
LinkedList<Edge> edges;
public Node(double x, double y, String name) {
circle = new Circle(10);
circle.setFill(Color.WHITE);
circle.setStroke(Color.BLACK);
text = new Text(name);
this.x = x;
this.y = y;
this.name = name;
isVisited = false;
edges = new LinkedList<>();
}
public boolean isVisited() {
return isVisited;
}
public void visitNode() {
isVisited = true;
}
public void leaveNode() {
isVisited = false;
}
public Circle getCircle(double scale) {
circle.setCenterX(x * scale);
circle.setCenterY(y * scale);
return circle;
}
public Circle getCircle() {
return circle;
}
public Text getText() {
text.layoutXProperty().bind(circle.centerXProperty().add(-text.getLayoutBounds().getWidth() / 2));
text.layoutYProperty().bind(circle.centerYProperty().add(5));
return text;
}
}
Edgecontains members that areNodes and classNodecontains a list ofEdges - even though memberedgesis never used in classNode. Are you certain that classNoderequires theedgesmember?