【问题标题】:No suitable method没有合适的方法
【发布时间】:2018-10-21 03:29:36
【问题描述】:

我有这个没有合适方法的错误,我不确定这意味着什么,错误在这一行 vertices.set(v, new ArrayList());

List<Vertex> vertices = new ArrayList<Vertex>();

public Vertex insertVertex(String n){
   Vertex v = new Vertex(n);
   vertices.set(v, new ArrayList<Edge>());    
   return n;
}

【问题讨论】:

  • 您的代码中的vertices 是什么?
  • vertices = new ArrayList();
  • 你知道什么是列表吗?你觉得set应该做什么?
  • ArrayList.set() 接受int 作为第一个参数,因为它是要设置的索引。您可能需要改用Map&lt;Vertex, List&lt;Edge&gt;&gt;

标签: java arrays list


【解决方案1】:

ArrayList.set 方法接受索引作为第一个参数。

如果你想将你的顶点添加到vertices,试试这个:

vertices.add(v);

如果您想存储顶点及其相邻边,请尝试使用一些 Map:

Map<Vertex, List<Edge>> vertices = new HashMap<Vertex, List<Edge>>();

public Vertex insertVertex(String n){
   Vertex v = new Vertex(n);
   vertices.put(v, new ArrayList<Edge>());    
   return n;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    • 2014-08-27
    • 2021-06-19
    • 1970-01-01
    • 2015-11-26
    • 2019-09-26
    相关资源
    最近更新 更多