【问题标题】:Adding vertices to graph using ArrayList使用 ArrayList 向图形添加顶点
【发布时间】:2013-11-24 21:21:42
【问题描述】:

我正在尝试添加创建两个顶点的 ArrayList(setA、setB),它们将存储商店以便后期比较它们,但是我无法将顶点添加到 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());
        }
    }
}

如果我在正确的方向上,如果有人可以指导我。

【问题讨论】:

  • 您正在尝试将int 添加到包含Vertexes 的ArrayList

标签: java arraylist


【解决方案1】:

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

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

【讨论】:

  • 这里如何实例化顶点?
  • 简单定义接受int的构造函数,比如:public Vertex(int v){ .... }`
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-07-04
  • 1970-01-01
  • 2016-08-28
  • 1970-01-01
相关资源
最近更新 更多