I'm not sure why my code isn't working. I'm trying to create a graph using an arraylist but this code doesn't appear to work. Whenever I try and get a node ID from the arraylist it returns 0. I'm sure I've just done something clumsy. Can ayone point out my mistake?
private ArrayList<Node> NodeList = new ArrayList<Node>();
public void addNode(int id, String Label, List connections) {
NodeList.add(new Station(id, Label, connections));
}
public ArrayList<Node> getNodes() {
return NodeList;
}
Then in my main method (these are just for testing purposes)
ArrayList<Integer> connections = new ArrayList<Integer>();
connections.add(2);
connections.add(5);
g.addNode(6, "first",connections );
System.out.println(""+g.getNodes().get(0).getID());
Thanks for the interest guys! Here is the station class:
private int id;
private String stopName;
private ArrayList connections;
public Station(int id, String stopName, List connection) {
id = this.id;
stopName = this.stopName;
setConnections(connection);
}
public List getConnections() {
return connections;
}
public int getID() {
return id;
}
public String getLabel() {
return stopName;
}
idof 0 which is why that is what you find.StationaNode?Station(int, String, List)(and possibly its super constructor),Node#getID()and/orStation#getID().