0

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;
    }
9
  • 2
    Can you provide an example we can run? Otherwise we can only guess you are using an id of 0 which is why that is what you find. Oct 12, 2013 at 22:39
  • Is a Station a Node? Oct 12, 2013 at 22:41
  • Don't forget to always use lower Camel Casing!
    – 0x6C38
    Oct 12, 2013 at 22:44
  • 1
    Please show the implementation of Station(int, String, List) (and possibly its super constructor), Node#getID() and/or Station#getID().
    – Koraktor
    Oct 12, 2013 at 22:45
  • @PeterLawrey added some more stuff, sorry.
    – Alex
    Oct 12, 2013 at 22:46

2 Answers 2

6

These are two mistakes:

id = this.id;
stopName = this.stopName;

It should be:

this.id = id;
this.stopName = stopName;

See, 'this' is used to refer to calling object. So when you write like above, you say that "This object's id = id (argument one)".

And when you write as you have written in your question,

id = this.id;

you are changing the value of 'passed argument id' and assigning it the value of object's id, whose default value is 0! That's why you were getting the result as 0.

3
  • Thanks, this is the solution. I'm an idiot :( sorry for wasting your time.
    – Alex
    Oct 12, 2013 at 22:57
  • no worries! It happens.. Hope your concept is clear now! Enjoy :)
    – kunal18
    Oct 12, 2013 at 23:01
  • 1
    @John Don't forget to mark the right answer so stalin get's his well deserved points ;)
    – Sentry
    Oct 15, 2013 at 22:36
2

In your constructor, the assignments are the wrong way around. You want

public Station(int id, String stopName, List connection) {
    this.id = id;
    this.stopName = stopName;
    setConnections(connection);
}

instead of

public Station(int id, String stopName, List connection) {
    id = this.id;
    stopName = this.stopName;
    setConnections(connection);
}
5
  • +1 id = this.id; is the wrong way around and copies the value the wrong way as well. Oct 12, 2013 at 22:56
  • You can enforce this by writing your constructor's signature as public Station(final int id, final String stopName, final List connection). Then, id = this.id will be a compile-time error, which you want. By the way, don't use raw Lists. And, watch out in your unshown setConnections method; you really want to create a new List, not share the input list. Oct 13, 2013 at 0:13
  • @EricJablow that sounds like a comment to the question asker, not to me. Oct 13, 2013 at 0:15
  • I'm not sure where to make that comment. The poster's question has no hook to make that comment. Oct 13, 2013 at 0:24
  • It certainly sounds like it's directed at the poster, and not at me. Oct 13, 2013 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.