有时,更有效的是在队列中定义您计划加到图中的顶点或边,然后使用一个简单的for loop来遍历队列,加增顶点或边。下面的例子使用loop循环直接增加了我们假想的机场到图中。注意我们不必指定每个顶点的ID。图会为每个新顶点分配一个唯一的ID。
Sometimes it is more efficient to define the details of the vertices or edges that you plan to add to
the graph in an array and then add each vertex or edge using a simple for loop that iterates over it.
The following example adds our imaginary airports directly to the graph using such a loop. Notice
that we do not have to specify the ID that we want each vertex to have. The graph will assign a
unique ID to each new vertex for us.

 

 像下面这样我们可以用遍历对象g来增加顶点。注意调用next() ,不调用next(),这个顶点的创建就和我们预期的不一样。

We could also have added the vertices using the traversal object g as follows. Notice the call to
next(). Without this the vertex creation will not work as expected.

通过HTTPl连接在远程的图上工作的时候,使用for循环来创建顶点和边是一项非常有用的技术。把一系列的创建步骤合并在一个单一的REST API调用中是非常方便的方式。

This technique of creating vertices and/or edges using a for loop can also be useful when working
with graphs remotely over HTTP connections. It is a very convenient way to combine a set of
creation steps into a single REST API call.
如果您喜欢Groovy化的句法,您也可以这么做:
If you prefer a more Groovy like syntax you can also do this.

 

 

posted on 2022-04-22 11:58  bokeyuannicheng0000  阅读(23)  评论(0)    收藏  举报