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. Commented Oct 12, 2013 at 22:39
  • Is a Station a Node? Commented Oct 12, 2013 at 22:41
  • Don't forget to always use lower Camel Casing! Commented 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(). Commented Oct 12, 2013 at 22:45
  • @PeterLawrey added some more stuff, sorry. Commented 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.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, this is the solution. I'm an idiot :( sorry for wasting your time.
no worries! It happens.. Hope your concept is clear now! Enjoy :)
@John Don't forget to mark the right answer so stalin get's his well deserved points ;)
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 Comments

+1 id = this.id; is the wrong way around and copies the value the wrong way as well.
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.
@EricJablow that sounds like a comment to the question asker, not to me.
I'm not sure where to make that comment. The poster's question has no hook to make that comment.
It certainly sounds like it's directed at the poster, and not at me.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.