I get different behaviour in Java depending on HOW I initialize the data structures
<p>I implemented an algorithm in Java. I coded two versions:</p>

<ul>
<li>one where I initialized the data structures in the constructor,
and</li>
<li>one where I parsed a textfile and initialized the data structure from the input</li>
</ul>

<p>The strange thing is that I got different behaviour from the two versions, and can hardly understand how.</p>

<p><b>Why do I get different behaviour?</b></p>

<p>The algorithm is a first part of Depth-First Search. A set of nodes should be visited and printed <b>only once</b>. In my version where I read from a textfile, the first node is printed twice. The program uses recursion.</p>

<p>Here is the output, the code is below. The first four lines prints the data structures, then is each first-visit-of-a-node printed, and a counter. The counter should only go to 2 <b>not</b> 3.</p>

<p><i>Output, when read from textfile:</i></p>

<pre><code>&gt;java GraphStart ex1.txt
Node 1
Node 2
Edge: Node 1 -- Node 2
Edge: Node 2 -- Node 1

Start on Node 1
Node 1 Counter: 1
Node 2 Counter: 2
Node 1 Counter: 3
</code></pre>

<p><i>Output, when initialized in constructor:</i></p>

<pre><code>Node 1
Node 2
Edge: Node 1 -- Node 2
Edge: Node 2 -- Node 1

Start on Node 1
Node 1 Counter: 1
Node 2 Counter: 2
</code></pre>

<p><i>Depth-First Search - initialized in contructor:</i></p>

<pre><code>public class DepthFirstSearch {
    private final static LinkedList&lt;Node&gt; nodes = new LinkedList&lt;Node&gt;();
    private static LinkedList[] edges = new LinkedList[0];

    public DepthFirstSearch() {

        Node node1 = new Node(1);
        Node node2 = new Node(2);

        nodes.add(node1);
        nodes.add(node2);

        edges = Arrays.copyOf(edges, 1);
        edges[0] = new LinkedList&lt;Edge&gt;();
        edges[0].add(new Edge(node1, node2));

        edges = Arrays.copyOf(edges, 2);
        edges[1] = new LinkedList&lt;Edge&gt;();
        edges[1].add(new Edge(node2, node1));

        DFS.startDFS(nodes, edges);
    }

    public static void main(String[] args) {
        new DepthFirstSearch();
    }
}
</code></pre>

<p><i>Depth-First Search - initialized from textfile:</i></p>

<pre><code>public class GraphStart {
    private final static LinkedList&lt;Node&gt; nodes = new LinkedList&lt;Node&gt;();
    private static LinkedList[] edges = new LinkedList[0];

    public GraphStart(String fileName) {
        scanFile(fileName);

        DFS.startDFS(nodes, edges);
    }

    // Parse a textfile with even number of integers
    // Add the nodes and edges to the datastructures
    private static void scanFile(String filename) {
        try {
            Scanner sc = new Scanner(new File(filename));

            while(sc.hasNextInt()){
                Node startNode = new Node(sc.nextInt());
                if(sc.hasNextInt()) {
                    Node endNode = new Node(sc.nextInt());

                    if(!nodes.contains(startNode)){
                        nodes.add(startNode);
                        //EDIT
                        System.out.println("Added " + startNode);

                        // Grow the Edge-array and initialize the content
                        if(edges.length &lt; startNode.getNr())
                            edges = Arrays.copyOf(edges, startNode.getNr());

                        edges[startNode.getNr()-1] = new LinkedList&lt;Edge&gt;();
                    }

                    if(!nodes.contains(endNode)){
                        nodes.add(endNode);
                        //EDIT
                        System.out.println("Added " + endNode);

                        // Grow the Edge-array and initialize the content
                        if(edges.length &lt; endNode.getNr())
                            edges = Arrays.copyOf(edges, endNode.getNr());

                        edges[endNode.getNr()-1] = new LinkedList&lt;Edge&gt;();
                    }

                    // Add the Edge
                    edges[startNode.getNr()-1].add(new Edge(startNode, endNode));
                }
            }
        } catch (FileNotFoundException e) {
            System.out.println("Can not find the file:" + filename);
            System.exit(0);
        }
    }

    public static void main(String[] args) {
        if(args.length==1) {
            new GraphStart(args[0]);
        } else {
            System.out.println("Wrong argument. &lt;filename&gt;");
        } 
    }
}
</code></pre>

<p><i>Textfile for input:</i></p>

<pre><code>1 2
2 1
</code></pre>

<p>It represents the Edge from Node 1 to Node 2, and the Edge from Node 2 to Node 1.</p>

<p>The algorithm is implemented in a static file, used by both versions.
<i>DFS - the algorithm:</i></p>

<pre><code>public class DFS {
    private static int counter = 0;
    private static LinkedList&lt;Node&gt; nodes;
    private static LinkedList[] edges;

    public static void startDFS(LinkedList&lt;Node&gt; ns, LinkedList[] es) {
        nodes = ns;
        edges = es;

        /* Print the data structures */ 
        printList(nodes);
        printEdges(edges);

        for(Node n : nodes) {
            if(!n.isVisited()) {
                System.out.println("\nStart on "+n);
                dfs(n);
            }
        }
    }

    private static void dfs(Node n) {
    counter++;
    n.visit();
    System.out.println(n + " Counter: "  + counter);
    for(Object o : edges[n.getNr()-1]) {
        if(!((Edge)o).getEnd().isVisited()) {
            dfs(((Edge)o).getEnd());
        }
    }

    private static void printList(LinkedList&lt;?&gt; list) {
        for(Object obj : list)
            System.out.println(obj);
    }

    private static void printEdges(LinkedList[] edges) {
        for(LinkedList list : edges) {
            System.out.print("Edge: ");
            for(Object o : list) {
                System.out.print(o);
            }
            System.out.println("");
        }
    }
}
</code></pre>

<p><b>EDIT:</b> Added code listings of Node and Edge.</p>

<p><i>Node:</i></p>

<pre><code>public class Node {
    private final int nr;
    private boolean visited = false;

    public Node(int nr) {
        this.nr = nr;
    }

    public int getNr()          { return nr; }
    public boolean isVisited()  { return visited; }
    public void visit()         { visited = true; }

    @Override
    public boolean equals(Object obj) {
        if(obj instanceof Node)
            return ((Node)obj).getNr() == nr;
        else
            return false;
        }

    @Override
    public String toString() {
        return "Node " + nr;
    }
}
</code></pre>

<p><i>Edge:</i></p>

<pre><code>public class Edge {
    private final Node startNode;
    private final Node endNode;

    public Edge(Node start, Node end) {
        this.startNode = start;
        this.endNode = end;
    }

    public Node getStart()      { return startNode; }
    public Node getEnd()        { return endNode; }

    public String toString() {
        return startNode + " " + 
            "--" + " " +
            endNode;
    }
}
</code></pre>

<p>Sorry for the very long code listings. I tried to isolate my problem and also show a runnable program.</p>
