我尝试迭代HashMap包含有向边的ArrayLists,用于Edgeweighted Directed Graph。我已按照类似问题的说明操作,但操作不正确。
我想: 遍历HashMap,检索每个ArrayList。 然后遍历ArrayList中的directedEdges。 打印出Directed Edge,它具有以下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();
}
}
以下是输出: 我可以看到Directed Edges正被添加到图形中,但只有一半在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
输出:A-> E 7 B-> C 4 C-> E 2 D-> E 6 E-> B 3&#34;
答案 0 :(得分:2)
问题是,在添加和边缘时,您总是创建一个新的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);
}