![](/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.