如何从 .txt 文件创建 Java 图形文件
我正在尝试创建一个图形文件。我必须从 .gra 文件(我认为是 .txt 文件)中读取值。我们被告知根据格式
,边缘也是如此,
我查看了几个相关的问题,但仍然找不到答案。
这是我得到的代码:
public EdgeListVertex(V element) {
this.element = element;
}
@Override
public V element() {
return element;
}
public String toString() {
return element.toString();
}
}
private class EdgeListEdge implements Edge<E> {
Position<EdgeListEdge> position;
E element;
EdgeListVertex start, end;
public EdgeListEdge(EdgeListVertex start, EdgeListVertex end, E element) {
this.start = start;
this.end = end;
this.element = element;
}
@Override
public E element() {
return element;
}
public String toString() {
return element.toString();
}
}
private List<EdgeListVertex> vertices;
private List<EdgeListEdge> edges;
public EdgeListGraph() {
vertices = new LinkedList<EdgeListVertex>();
edges = new LinkedList<EdgeListEdge>();
}
@SuppressWarnings("unchecked")
@Override
public Vertex<V>[] endVertices(Edge<E> e) {
EdgeListEdge edge = (EdgeListEdge) e;
Vertex<V>[] endpoints = (Vertex<V>[]) new Vertex[2];
endpoints[0] = edge.start;
endpoints[1] = edge.end;
return endpoints;
}
@Override
public Vertex<V> opposite(Vertex<V> v, Edge<E> e) {
Vertex<V>[] endpoints = endVertices(e);
if (endpoints[0].equals(v)) {
return endpoints[1];
} else if (endpoints[1].equals(v)) {
return endpoints[0];
}
throw new InvalidVertexException();
}
@Override
public boolean areAdjacent(Vertex<V> v, Vertex<V> w) {
for (EdgeListEdge edge: edges) {
if ((edge.start.equals(v)) && (edge.end.equals(w))) return true;
if ((edge.end.equals(v)) && (edge.start.equals(w))) return true;
}
return false;
}
@Override
public V replace(Vertex<V> v, V x) {
EdgeListVertex vertex = (EdgeListVertex) v;
V temp = vertex.element;
vertex.element = x;
return temp;
}
@Override
public E replace(Edge<E> e, E x) {
EdgeListEdge edge = (EdgeListEdge) e;
E temp = edge.element;
edge.element = x;
return temp;
}
@Override
public Vertex<V> insertVertex(V v) {
EdgeListVertex vertex = new EdgeListVertex(v);
Position<EdgeListVertex> position = vertices.insertLast(vertex);
vertex.position = position;
return vertex;
}
@Override
public Edge<E> insertEdge(Vertex<V> v, Vertex<V> w, E o) {
EdgeListEdge edge = new EdgeListEdge((EdgeListVertex) v, (EdgeListVertex) w, o);
Position<EdgeListEdge> position = edges.insertLast(edge);
edge.position = position;
return edge;
}
@Override
public V removeVertex(Vertex<V> v) {
Iterator<Edge<E>> it = incidentEdges(v).iterator();
while (it.hasNext()) it.remove();
EdgeListVertex vertex = (EdgeListVertex) v;
vertices.remove(vertex.position);
return vertex.element;
}
@Override
public E removeEdge(Edge<E> e) {
EdgeListEdge edge = (EdgeListEdge) e;
edges.remove(edge.position);
return edge.element;
}
@Override
public List<Edge<E>> incidentEdges(Vertex<V> v) {
LinkedList<Edge<E>> list = new LinkedList<Edge<E>>();
for (EdgeListEdge edge : edges) {
if (edge.start.equals(v)) list.insertLast(edge);
if (edge.end.equals(v)) list.insertLast(edge);
}
return list;
}
@Override
public List<Vertex<V>> vertices() {
LinkedList<Vertex<V>> list = new LinkedList<Vertex<V>>();
for (EdgeListVertex vertex : vertices) {
list.insertLast(vertex);
}
return list;
}
@Override
public List<Edge<E>> edges() {
LinkedList<Edge<E>> list = new LinkedList<Edge<E>>();
for (EdgeListEdge edge : edges) {
list.insertLast(edge);
}
return list;
}
}
有什么提示吗?
I am trying to create a graph file .I have to read in values from a .gra file(which I think is a .txt file).We were told to tokenise lines based on a space in the format <vertex> <name> <x-coord> <y-coord>
,same for edge
I had a look at a couple of related questions,but still cant find the answer.
Here's the code I was given:
public EdgeListVertex(V element) {
this.element = element;
}
@Override
public V element() {
return element;
}
public String toString() {
return element.toString();
}
}
private class EdgeListEdge implements Edge<E> {
Position<EdgeListEdge> position;
E element;
EdgeListVertex start, end;
public EdgeListEdge(EdgeListVertex start, EdgeListVertex end, E element) {
this.start = start;
this.end = end;
this.element = element;
}
@Override
public E element() {
return element;
}
public String toString() {
return element.toString();
}
}
private List<EdgeListVertex> vertices;
private List<EdgeListEdge> edges;
public EdgeListGraph() {
vertices = new LinkedList<EdgeListVertex>();
edges = new LinkedList<EdgeListEdge>();
}
@SuppressWarnings("unchecked")
@Override
public Vertex<V>[] endVertices(Edge<E> e) {
EdgeListEdge edge = (EdgeListEdge) e;
Vertex<V>[] endpoints = (Vertex<V>[]) new Vertex[2];
endpoints[0] = edge.start;
endpoints[1] = edge.end;
return endpoints;
}
@Override
public Vertex<V> opposite(Vertex<V> v, Edge<E> e) {
Vertex<V>[] endpoints = endVertices(e);
if (endpoints[0].equals(v)) {
return endpoints[1];
} else if (endpoints[1].equals(v)) {
return endpoints[0];
}
throw new InvalidVertexException();
}
@Override
public boolean areAdjacent(Vertex<V> v, Vertex<V> w) {
for (EdgeListEdge edge: edges) {
if ((edge.start.equals(v)) && (edge.end.equals(w))) return true;
if ((edge.end.equals(v)) && (edge.start.equals(w))) return true;
}
return false;
}
@Override
public V replace(Vertex<V> v, V x) {
EdgeListVertex vertex = (EdgeListVertex) v;
V temp = vertex.element;
vertex.element = x;
return temp;
}
@Override
public E replace(Edge<E> e, E x) {
EdgeListEdge edge = (EdgeListEdge) e;
E temp = edge.element;
edge.element = x;
return temp;
}
@Override
public Vertex<V> insertVertex(V v) {
EdgeListVertex vertex = new EdgeListVertex(v);
Position<EdgeListVertex> position = vertices.insertLast(vertex);
vertex.position = position;
return vertex;
}
@Override
public Edge<E> insertEdge(Vertex<V> v, Vertex<V> w, E o) {
EdgeListEdge edge = new EdgeListEdge((EdgeListVertex) v, (EdgeListVertex) w, o);
Position<EdgeListEdge> position = edges.insertLast(edge);
edge.position = position;
return edge;
}
@Override
public V removeVertex(Vertex<V> v) {
Iterator<Edge<E>> it = incidentEdges(v).iterator();
while (it.hasNext()) it.remove();
EdgeListVertex vertex = (EdgeListVertex) v;
vertices.remove(vertex.position);
return vertex.element;
}
@Override
public E removeEdge(Edge<E> e) {
EdgeListEdge edge = (EdgeListEdge) e;
edges.remove(edge.position);
return edge.element;
}
@Override
public List<Edge<E>> incidentEdges(Vertex<V> v) {
LinkedList<Edge<E>> list = new LinkedList<Edge<E>>();
for (EdgeListEdge edge : edges) {
if (edge.start.equals(v)) list.insertLast(edge);
if (edge.end.equals(v)) list.insertLast(edge);
}
return list;
}
@Override
public List<Vertex<V>> vertices() {
LinkedList<Vertex<V>> list = new LinkedList<Vertex<V>>();
for (EdgeListVertex vertex : vertices) {
list.insertLast(vertex);
}
return list;
}
@Override
public List<Edge<E>> edges() {
LinkedList<Edge<E>> list = new LinkedList<Edge<E>>();
for (EdgeListEdge edge : edges) {
list.insertLast(edge);
}
return list;
}
}
Any tips?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个好的方法可能是创建一个包含顶点、名称和坐标字段的新类。然后将扫描仪中的数据读入 ArrayList 。之后,您可以将 ArrayList 传递到您用于绘图的任何内容中(这可能需要一些调整)。
示例扫描仪代码(未经测试):
然后,从扫描仪读取(未经测试):
我希望这会有所帮助。
A good approach might be to create a new class with fields for vertex, name and coordinates. Then read in the data from a scanner into an ArrayList of the class you just created. After this, you could just pass the ArrayList into whatever you are using for graphing (which might require a few tweaks).
Example scanner code (untested):
Then, to read from the scanner (untested):
I hope this helps.