Java-更改图形节点的标签不会更新连接中的标签

Java-更改图形节点的标签不会更新连接中的标签,java,data-structures,Java,Data Structures,我有一个非常简单的图形实现,如下所示: class Graph{ ArrayList<Node> nodes; ... public void addNode(Node n){ nodes.add(n); } void changeLabel(int index, char label){ nodes.get(index).label = label; } } clas

我有一个非常简单的图形实现,如下所示:

class Graph{    
    ArrayList<Node> nodes;
    ...

    public void addNode(Node n){
        nodes.add(n);
    }

    void changeLabel(int index, char label){
        nodes.get(index).label = label;
    }           
}

class Node{
    char label;
    ArrayList<Node> connections;

    public void addConnection(Node other){
        connections.add(other);
    }    
}
Graph g = new Graph();
Node a = new Node('A');
Node b = new Node('B');
Node c = new Node('C');

//code to add nodes to graph
g.addNode(a);
g.addNode(b);
g.addNode(c);

//code to add each node as connection to every other node
a.addConnection(b);
a.addConnection(c);
b.addConnection(a);
b.addConnection(c);
....
然后,我更改其中一个图形节点上的标签(例如A到E):


现在,当我显示图形节点时,我可以看到更新的标签。但是,当我遍历节点的连接时,仍然会得到标签“A”。为什么会这样?

我确信问题不在您发布的代码片段中。下面的程序工作正常。试着把它和你的相匹配

import java.util.List;
import java.util.ArrayList;
public class Temp_1 {
  public static void main(String[] args) {
    Graph graph = new Graph();
      graph.nodes = new ArrayList<Node>();
      //add nodes
      Node node1 = new Node();
      node1.label = 'A';

      Node node2 = new Node();
      node2.label = 'B';

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

      printNodes(graph);
      graph.nodes.get(0).addConnection(graph.nodes.get(1));
      graph.nodes.get(1).addConnection(graph.nodes.get(0));

      printConnections(graph);


      graph.changeLabel(0, '1');

      System.out.println("after changing label");
      printNodes(graph);
      printConnections(graph);

  }

  static void printNodes(Graph g) {
    System.out.println("Printing Nodes ");
    for (Node elem_ : g.nodes) System.out.println(elem_.label);
  }

  static void printConnections(Graph g) {

    System.out.println("Printing Connections ");
    for (Node elem_ : g.nodes) {
      System.out.println("Printing Connections for node [" + elem_.label + "]");
      for (Node connection_ : elem_.connections) {
        System.out.println(connection_.label);
      }
    }

  }
}


class Graph{

public ArrayList<Node> nodes;


void changeLabel(int index, char label){
    nodes.get(index).label = label;
  }

}

class Node{

char label;
ArrayList<Node> connections = new ArrayList<Node>();

public void addConnection(Node other){
    connections.add(other);
}

}
import java.util.List;
导入java.util.ArrayList;
公共类临时工1{
公共静态void main(字符串[]args){
图形=新图形();
graph.nodes=新的ArrayList();
//添加节点
Node node1=新节点();
node1.label='A';
Node node2=新节点();
node2.label='B';
graph.nodes.add(node1);
graph.nodes.add(node2);
打印节点(图);
graph.nodes.get(0).addConnection(graph.nodes.get(1));
graph.nodes.get(1).addConnection(graph.nodes.get(0));
打印连接(图形);
图.changeLabel(0,'1');
System.out.println(“更改标签后”);
打印节点(图);
打印连接(图形);
}
静态void打印节点(图g){
System.out.println(“打印节点”);
对于(节点元素:g.nodes)System.out.println(元素标签);
}
静态连接(图g){
System.out.println(“打印连接”);
用于(节点元素:g.nodes){
System.out.println(“打印节点[“+elem_u2;.label+”]”的连接);
用于(节点连接:元素连接){
系统输出打印LN(连接标签);
}
}
}
}
类图{
公共阵列列表节点;
void changelab(int索引、char标签){
nodes.get(index).label=label;
}
}
类节点{
字符标签;
ArrayList connections=新建ArrayList();
公共无效添加连接(节点其他){
连接。添加(其他);
}
}

哦,对不起,我忘了指定添加新连接的方式。我已经编辑了添加连接的代码。不确定这是否有什么区别。你能不能也发布Graph类的addNode方法。嗯,我好像犯了一个错误。很抱歉谢谢。
import java.util.List;
import java.util.ArrayList;
public class Temp_1 {
  public static void main(String[] args) {
    Graph graph = new Graph();
      graph.nodes = new ArrayList<Node>();
      //add nodes
      Node node1 = new Node();
      node1.label = 'A';

      Node node2 = new Node();
      node2.label = 'B';

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

      printNodes(graph);
      graph.nodes.get(0).addConnection(graph.nodes.get(1));
      graph.nodes.get(1).addConnection(graph.nodes.get(0));

      printConnections(graph);


      graph.changeLabel(0, '1');

      System.out.println("after changing label");
      printNodes(graph);
      printConnections(graph);

  }

  static void printNodes(Graph g) {
    System.out.println("Printing Nodes ");
    for (Node elem_ : g.nodes) System.out.println(elem_.label);
  }

  static void printConnections(Graph g) {

    System.out.println("Printing Connections ");
    for (Node elem_ : g.nodes) {
      System.out.println("Printing Connections for node [" + elem_.label + "]");
      for (Node connection_ : elem_.connections) {
        System.out.println(connection_.label);
      }
    }

  }
}


class Graph{

public ArrayList<Node> nodes;


void changeLabel(int index, char label){
    nodes.get(index).label = label;
  }

}

class Node{

char label;
ArrayList<Node> connections = new ArrayList<Node>();

public void addConnection(Node other){
    connections.add(other);
}

}