如何从 .t​​xt 文件创建 Java 图形文件

发布于 2024-11-02 18:11:14 字数 4672 浏览 0 评论 0原文

我正在尝试创建一个图形文件。我必须从 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

讽刺将军 2024-11-09 18:11:14

一个好的方法可能是创建一个包含顶点、名称和坐标字段的新类。然后将扫描仪中的数据读入 ArrayList 。之后,您可以将 ArrayList 传递到您用于绘图的任何内容中(这可能需要一些调整)。

示例扫描仪代码(未经测试):

File graphFile = new File(filepath);
Scanner graphScanner = new Scanner(graphFile);

然后,从扫描仪读取(未经测试):

ArrayList<graphClass> dataPoints = new ArrayList<graphClass>(); //you will have to make graphClass
while(graphScanner.hasNextLine()) {
    dataPoints.add(new graphClass(graphScanner.nextDouble(), graphScanner.next(), graphScanner.nextDouble(), graphScanner.nextDouble()); //assuming constructor of graphClass takes vertex, name, x, y in that order

我希望这会有所帮助。

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):

File graphFile = new File(filepath);
Scanner graphScanner = new Scanner(graphFile);

Then, to read from the scanner (untested):

ArrayList<graphClass> dataPoints = new ArrayList<graphClass>(); //you will have to make graphClass
while(graphScanner.hasNextLine()) {
    dataPoints.add(new graphClass(graphScanner.nextDouble(), graphScanner.next(), graphScanner.nextDouble(), graphScanner.nextDouble()); //assuming constructor of graphClass takes vertex, name, x, y in that order

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文