Java 读取顶点并创建边

Java 读取顶点并创建边,java,algorithm,Java,Algorithm,对于这个任务,我应该创建一个“想要的”网络,它应该是一个图。您读取的第一个文件包含一个文本文件,其中包含从/到的顶点以及一些其他数据。我还有一个内部计数器,它计算使用了多少次addVertex。到目前为止,它是正确的,测试打印是正确的,但是当我运行它时,它在列表中没有顶点,甚至很难,它说它已经添加了。 有什么想法为什么我不会添加到它的列表和任何想法 我是这样读的: static Graph graph; private static void createNetwork(String fil1

对于这个任务,我应该创建一个“想要的”网络,它应该是一个图。您读取的第一个文件包含一个文本文件,其中包含从/到的顶点以及一些其他数据。我还有一个内部计数器,它计算使用了多少次
addVertex
。到目前为止,它是正确的,测试打印是正确的,但是当我运行它时,它在列表中没有顶点,甚至很难,它说它已经添加了。 有什么想法为什么我不会添加到它的列表和任何想法

我是这样读的:

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

    }
}

类图{
公共静态整数大小;
HashMap图;
受保护的垂直列表;
受保护的列表边缘;
//Brukes-til-Dijkstras算法
公开名单kjent;
公开名单ukjent;
公共图(){
graph=新的HashMap();
kjent=newarraylist();
ukjent=新的ArrayList();
Vertices=新的ArrayList();
边=新的ArrayList();
}
}

它们首先不会添加到列表中
addVertex()
打印出关于要添加到列表中的顶点的消息,尽管它还没有这样做。然后,它尝试执行,但失败了,导致异常被
ArrayList.add()
抛出,异常被捕获在
createNetwork()
中,因此您不会注意到出现了问题

不要捕捉无法处理的异常。在执行操作之前,不要记录操作

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>();
    }

}