java - 迭代有向边的ArrayList的HashMap,提取有向边数据

标签 java arraylist graph iterator hashmap

我正在尝试迭代包含有向边的 ArrayList 的 HashMap,以获得边加权有向图。我已按照类似问题的说明进行操作,但它无法正常工作。

我想要: 遍历HashMap,检索每个ArrayList。 然后迭代ArrayList 中的directedEdges。 打印出有向边,其中有以下 toString 方法:

public String toString(){  
    return String.format("%s->%s %d", v, w, weight);
     }

在完整的 EdgeDirectedGraph 类下面,我正在查询的 toString 方法位于底部。

import java.util.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class EdgeWeightedDiGraph{
private static final String NEWLINE = System.getProperty("line.separator");
private int V;
private int E;
private HashMap<String, ArrayList<DirectedEdge>> adjacencyList;

public EdgeWeightedDiGraph(String filename, String delim){  
  BufferedReader br = null;
      try{
         br = new BufferedReader(new FileReader(filename));
         E = 0;
         V = Integer.parseInt(br.readLine());
         //System.out.println(V);
         String line = null;
         this.adjacencyList = new HashMap<String, ArrayList<DirectedEdge>>();
         while ((line = br.readLine()) != null) {
             //adj = (Bag<Integer>[]) new Bag[V];
             String arr[] = line.split(delim); // properties
             String v = arr[0];
             String w = arr[1];
             int e =  Integer.parseInt(arr[2]);
             DirectedEdge dEdge = new DirectedEdge(v, w, e); 
             addEdge(dEdge);
             System.out.println(dEdge.from()+dEdge.to()+dEdge.weight());
             }   
          }  
      catch (FileNotFoundException ex) {
             System.out.println("Error: File not found");
             Logger.getLogger(EdgeWeightedDiGraph.class.getName()).log(Level.SEVERE, null, ex);
            }       
      catch (IOException ex) {
             Logger.getLogger(EdgeWeightedDiGraph.class.getName()).log(Level.SEVERE, null, ex);
            }
      finally{
         try {
             br.close();
         }
         catch (IOException ex) {
             Logger.getLogger(EdgeWeightedDiGraph.class.getName()).log(Level.SEVERE, null, ex);
         }
     }

}

  public HashMap getMap(){
      return adjacencyList;
  }

 public int V(){  
     return V;  
 }
 public int E(){  
     return E;  
 }
 public void addEdge(DirectedEdge e)
 {
     String v = e.from();
     String w = e.to();
     ArrayList<DirectedEdge> item = new ArrayList<DirectedEdge>();
     item.add(e);
     adjacencyList.put(v, item);
     System.out.println(v+item);
     E++; 
 }

 public Iterable<DirectedEdge> adjacencyList(String v){  
     return adjacencyList.get(v);  
 }

 public Iterable<DirectedEdge> edges()
 {

public String toString() {
      StringBuilder s = new StringBuilder();
      s.append(V + " " + E + NEWLINE);
      for (HashMap.Entry<String, ArrayList<DirectedEdge>> entry : adjacencyList.entrySet()){
          ArrayList<DirectedEdge> arrList = entry.getValue();
          for(DirectedEdge e :  arrList){
              s.append(e + "  "); }
          s.append(NEWLINE);
       }
      return s.toString();
  }
}

下面是输出: 我可以看到有向边被添加到图中,但只有一半在 toString() 方法中打印出来。

Print out of edges being added
A[A->B 5]
AB5, 
B[B->C 4] 
BC4, 
C[C->D 8] 
CD8, 
D[D->C 8]
DC8, 
D[D->E 6] 
DE6 ,
A[A->D 5] 
AD5, 
C[C->E 2]
CE2, 
E[E->B 3] 
EB3, 
A[A->E 7] 
AE7 

“toString()输出:A->E 7 B->C 4 C->E 2 D->E 6 E->B 3”

最佳答案

问题是,当您添加和边缘时,您总是创建一个新的 DirectedEdge 列表,而不是使用现有列表(如果存在)。

public void addEdge(DirectedEdge e)
{
    String v = e.from();
    String w = e.to();

    // Check if there is already an edge array
    ArrayList<DirectedEdge> item = adjacencyList.get(v);
    if(item == null) {
        // Does not exist, create it
        item = new ArrayList<DirectedEdge>();
    }
    item.add(e);
    adjacencyList.put(v, item);
    System.out.println(v+item);
}

关于java - 迭代有向边的ArrayList的HashMap,提取有向边数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32408893/

相关文章:

java - 如何从数组/ArrayList中获取特定元素?

java - 如何将数据库中的数据放入Arraylist中?

Java图形库

java - JButton 切换禁用和启用 ItemListener

java - 在执行 org.codehaus.mojo :exec-maven-plugin:1. 6.0 :exec: java. lang.String 时无法转换为 org.codehaus.mojo.exec.Modulepath

java - 识别包含 300k+ 字符串的列表中的重复元素

algorithm - 从图形中获取主线

javascript - facebook graph api 中的节点 ID 是什么,我如何找到它?

用于通过网络摄像头捕获图像的java代码UnsatisfiedLinkError

java - 为什么ObjectOutputStream写入14个字节而不是DataOutputStream写入8个字节?