0

Trying to add an incremented class member variable 'nNodeIndex' to arraylist, but at the end of the loop all the values stored are identical to the last iteration increment??

Class with member variable:

import java.util.ArrayList;

public class Graph {

    private static ArrayList<GraphNode> Nodes = new ArrayList<GraphNode>();
    private static ArrayList<GraphEdge> Edges = new ArrayList<GraphEdge>();

    private static boolean diGraph;
    private static int nNodeIndex;

    private boolean uniqueEdge(int from, int to)
    {
        for(int i=0; i< Edges.size(); i++){
          if(Edges.get(i).from() == from && Edges.get(i).to() == to)
              return false;
        }
        return true;
    }

    private void removeInvalidEdges(int nodeIndex)
    {
        for(int i=0; i<Edges.size(); i++)
        {
            if(Edges.get(i).from() == nodeIndex)
                Edges.remove(i);
            if(Edges.get(i).to() == nodeIndex)
                Edges.remove(i);                    
        }
    }

    Graph(boolean directionalGraph)
    {
        diGraph     = directionalGraph;
        nNodeIndex  = 1;
    }

    public GraphNode getNode(int nodeIndex)
    {
        return Nodes.get(nodeIndex);
    }

    public int getIndexByVector(int x, int y)
    {
        for(int i=0; i<Nodes.size(); i++)
        {
            if(Nodes.get(i).x() == x && Nodes.get(i).y() == y)
                return i;
        }
        return -1;
    }

    public int nextFreeNodeIndex(){ return nNodeIndex; }

    public void addNode(GraphNode node)
    {
        if(Nodes.size() > 0)
            Nodes.add(node.index()-1, node);
        else
            Nodes.add(node);

        nNodeIndex++;  
    }

    public void removeNode(int index)
    {
        Nodes.get(index).setIndex(-1);

        removeInvalidEdges(index);
    }

    public void addEdge(GraphEdge edge)
    {
        if(uniqueEdge(edge.from(), edge.to()))
            Edges.add(edge);

        if(!diGraph)
        {
            if(uniqueEdge(edge.to(), edge.from()))
                Edges.add(new GraphEdge(edge.to(), edge.from()));
        }
    }

    public void removeEdge(GraphEdge edge)
    {
        for(int i=0; i<Edges.size(); i++)
            if(Edges.get(i).from() == edge.from() &&
               Edges.get(i).to() == edge.to())
            {
                Edges.remove(i);
            }
    }

    public int totalNodes(){ return Nodes.size(); }

    public int totalEdges(){ return Edges.size(); }

    public int nActiveNodes()
    {
        int count = 0;

        for(int i=0; i<Nodes.size(); i++)
        {
            if(Nodes.get(i).index() != -1 )
                count++;
        }

        return count;
    }

    public void reset()
    {
        nNodeIndex = 1;
        Nodes.clear();
        Edges.clear();
    }

}

Use within main class:

  for(int row = 1; row <= boardWidth; row++){
       for(int col = 1; col <= boardHeight; col++){
          graph.addNode(new GraphNode(graph.nextFreeNodeIndex(), row, col));
       }
   }

Graph Node class:

public class GraphNode extends Square {

    private static int indexNum;

    GraphNode(int n, int x, int y)
    {
        super(x,y);
        indexNum = n;
    }

    public int index(){ return indexNum; }
    public void setIndex(int index){ indexNum = index; }
}

1 Answer 1

1

Do you need to show us the GraphNode class? I wonder if the node index held by it (set by the first parameter passed in its constructor) is a static variable.

3
  • I've added the graph node class, the node index first passed to the constructor is indeed a static variable.. Commented Nov 28, 2010 at 14:29
  • Then indeed that's likely your problem. If it is static, it will be the same for all GraphNode instances. Definitely make it be an instance or non-static variable. Commented Nov 28, 2010 at 14:46
  • Changed indexNum to non-static in the GraphNode class an it's working exactly as it should be... Commented Nov 28, 2010 at 14:51

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.