使用 ArrayList 向图形添加顶点 [英] Adding vertices to graph using ArrayList

查看:47
本文介绍了使用 ArrayList 向图形添加顶点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试添加创建两个顶点数组列表 (setA, setB),它将存储存储以便在后期比较它们,但我无法将顶点添加到数组列表中.

I am trying to add create two ArrayList of vertices (setA, setB) which will store the stores for comparing them late how ever I am unable to add vertices to the arraylist.

这是代码

import java.util.*;


public class BipartiteGraph<Vertex> {

    private String strName;
    ArrayList<Vertex>[] vertexList;

    public BipartiteGraph(){

        vertexList = new ArrayList[2];
        vertexList[0] = new ArrayList<Vertex>();
        vertexList[1] = new ArrayList<Vertex>();
        Scanner vertexInput = new Scanner(System.in);
        int vertex;
        vertex = vertexInput.nextInt();
        for(int i = 0; i < 10; i++){
            vertexList[0].add(vertexInput.nextInt());
        }
    }
}

如果我的方向正确,如果有人可以指导我.

Also if someone could guide me if I am in the right direction.

推荐答案

您正在尝试将 int 变量添加到 Vertex 对象的容器中.假设您的 Vertex 有一个接受 int 的构造函数,您应该使用:

You are trying to add int variable into the container of Vertex objects. Assuming, that your Vertex has a constructor accepting int you should rather use:

vertexList[0].add(new Vertex(vertexInput.nextInt()));

这篇关于使用 ArrayList 向图形添加顶点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆