Java 遍历有向边的ArrayList的HashMap,提取有向边数据
我试图遍历一个包含有向边的数组列表的HashMap,对于一个Edgeweighted有向图。我已经按照类似问题的说明进行了操作,但它不能正常工作 我想: 迭代HashMap,检索每个ArrayList。 然后遍历ArrayList中的DirectedEdge。 打印出具有以下toString方法的定向边:Java 遍历有向边的ArrayList的HashMap,提取有向边数据,java,arraylist,graph,iterator,hashmap,Java,Arraylist,Graph,Iterator,Hashmap,我试图遍历一个包含有向边的数组列表的HashMap,对于一个Edgeweighted有向图。我已经按照类似问题的说明进行了操作,但它不能正常工作 我想: 迭代HashMap,检索每个ArrayList。 然后遍历ArrayList中的DirectedEdge。 打印出具有以下toString方法的定向边: public String toString(){ return String.format("%s->%s %d", v, w, weight); } 在完整的
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()输出:A->e7b->c4c->e2d->e6e->b3”问题是,在添加和边缘时,您总是创建一个新的
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);
}
public void addEdge(directedge e)
{
字符串v=e.from();
字符串w=e.to();
//检查是否已经存在边缘阵列
ArrayList item=adjacencyList.get(v);
如果(项==null){
//不存在,请创建它
item=newarraylist();
}
项目.添加(e);
邻接列表放置(v,项);
系统输出打印项次(v+项);
}
很抱歉,我在尝试添加其余输出时遇到格式问题,然后它被弄乱了,现在我显然无法编辑90分钟?尝试从toString()添加输出:A->E 7 B->C 4 C->E 2 D->E 6 E->B 3当你标记我时,请你重新标记,因为我正在拼命尝试修复它,我一直收到关于代码的错误消息,试图在上面的评论中包含结果,所以我一直在做一些事情。很好,现在有两个人把我记下来了,这是为了同一件事吗?因为我真的不想等两天才使用赏金,当时我在使用格式时遇到了麻烦谢谢@HovercraftFullOfEels。我现在已经包括了全班,希望这不是太多的信息。
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);
}