Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用ArrayList表示邻接表的加权图_Java_Graph_Adjacency List - Fatal编程技术网

Java 用ArrayList表示邻接表的加权图

Java 用ArrayList表示邻接表的加权图,java,graph,adjacency-list,Java,Graph,Adjacency List,当有人使用ArrayList实现带有邻接列表表示的加权图时,我感到困惑。例如,在中,代码是 List<List<Node> > adj = new ArrayList<List<Node> >(); // Initialize list for every node for (int i = 0; i < V; i++) { List<Node> item = new ArrayList<Node>()

当有人使用ArrayList实现带有邻接列表表示的加权图时,我感到困惑。例如,在中,代码是

List<List<Node> > adj = new ArrayList<List<Node> >(); 

// Initialize list for every node 
for (int i = 0; i < V; i++) { 
    List<Node> item = new ArrayList<Node>(); 
    adj.add(item); 
} 

// Inputs for the DPQ graph 
adj.get(0).add(new Node(1, 9)); 
adj.get(0).add(new Node(2, 6)); 
adj.get(0).add(new Node(3, 5)); 
adj.get(0).add(new Node(4, 3)); 

adj.get(2).add(new Node(1, 2)); 
adj.get(2).add(new Node(3, 4)); 
应该是

adj.get(0).add(new Node(1, 4)); 
因为边0和1之间的权重是4,而不是9


我遗漏了什么?

我认为代码创建的示例与图片中的示例不同。谢谢Loris!你说得对。这张图片在我忽略的文章中有不同的输出。我没有跳出框框思考。
adj.get(0).add(new Node(1, 4));