问题将增加的类成员变量添加到arraylist内部循环中

时间:2010-11-28 00:59:49

标签: java arraylist

尝试将增加的类成员变量'nNodeIndex'添加到arraylist,但是在循环结束时,所有存储的值都与上一次迭代增量相同?

具有成员变量的类:

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();
    }

}

在主要课程中使用:

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

图形节点类:

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 个答案:

答案 0 :(得分:1)

您需要向我们展示GraphNode类吗?我想知道它所持有的节点索引(由其构造函数中传递的第一个参数设置)是否是一个静态变量。