![](/img/trans.png)
[英]How to prevent creating duplicates of edges between the same vertices in OrientDB?
[英]read vertices and creating edges
為此,我應該創建一個“ wannabe”網絡,該網絡應該是一個圖形。 您讀取的第一個文件包含一個文本文件,該文件包含從/到頂點的頂點以及其他一些數據。 我還有一個內部計數器,該計數器計算使用了addVertex
次數。 到目前為止,它是正確的並且測試打印是正確的,但是當我運行它時,列表中甚至沒有任何頂點,甚至說它已經被添加。 為什么id不會添加到列表中?
這是我的讀法:
static Graph graph;
private static void createNetwork(String fil1) {
try {
Scanner sc = new Scanner(new File(fil1));
graph = new Graph();
while (sc.hasNextLine()) {
String line = sc.nextLine();
String[] split = line.split("\t");
int[] connections = new int[split.length];
// System.out.println(line); // test utskrift
for (int i = 0; i < split.length; i++) {
connections[i] = Integer.parseInt(split[i].trim());
}
graph.addVertex(connections);
}
} catch (Exception e) {
}
}
以及其他一些正在調用的方法:
public void addVertex(int[] cons) {//, int tid, int ore) {
if (cons == null) {
return;
}
boolean added = false;
Vertex fra, til;
int tid = cons[2];
int ore = cons[3];
fra = new Vertex(cons[0], cons[1], cons[2], cons[3]);
til = new Vertex(cons[1], cons[0], cons[2], cons[3]);
if (verticies.contains(fra) == false) { //, tid, ore)
System.out.println(
fra.id + " --> " + til.id + " Ble lagt til i lista! " + size);
size++;
added = verticies.add(fra); //, tid, ore
// addEdge(fra, til, tid, ore);
// addEdge(til, fra, tid, ore);
// addBiEdges(fra, til, tid, ore);
// return true;
}
}
public boolean addBiEdges(Vertex fra, Vertex til, int tid, int ore) throws IllegalArgumentException {
return false; // addEdge(fra, til, tid, ore) && addEdge(til, fra, tid, ore);
}
public void addEdge(Vertex fra, Vertex til, int tid, int ore) throws IllegalArgumentException {
if (verticies.contains(fra) == false)
throw new IllegalArgumentException(fra.id + " er ikke med i grafen!");
if (verticies.contains(til) == false)
throw new IllegalArgumentException(til.id + " er ikke med i grafen!");
Edge e = new Edge(fra, til, tid, ore);
if (fra.findEdge(til) != null) {
return;
} else {
fra.addEdges(e);
til.addEdges(e);
edges.add(e);
// return true;
}
}
class Graph {
public static int size;
HashMap<Integer, Vertex> graph;
protected List<Vertex> verticies;
protected List<Edge> edges;
// Brukes til Dijkstras algoritmen
public List<Vertex> kjent;
public List<Vertex> ukjent;
public Graph() {
graph = new HashMap<Integer, Vertex>();
kjent = new ArrayList<Vertex>();
ukjent = new ArrayList<Vertex>();
verticies = new ArrayList<Vertex>();
edges = new ArrayList<Edge>();
}
}
它們不會首先添加到列表中。 addVertex()
打印出有關將頂點添加到列表的消息,盡管尚未這樣做。 然后,它嘗試但失敗,導致ArrayList.add()
引發異常。該異常被createNetwork()
捕獲,因此您不會注意到出現了問題。
不要捕獲您不會處理的異常。 在執行操作之前,請勿記錄它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.