gpt4 book ai didi

java - 在哈希表中存储和检索类的实例

转载 作者:行者123 更新时间:2023-12-01 12:40:14 25 4
gpt4 key购买 nike

所以我尝试在哈希表中存储类的实例:

 public void insertEdge(String val1, String val2, int len) {
Hashtable ref = new Hashtable(10, 3);

if(!ref.containsKey(val2))
{
Vert temp = new Vert(val2);
ref.put(val2, temp);
curNod++;
}

ref.get(val1).firstEdge = new Edge(ref.get(val2), ref.get(val1).firstEdge, len);
}

当我运行上面的程序时,编译器返回以下错误:

error: cannot find symbol

ref.get(val1).firstEdge = new Edge(ref.get(val2), ref.get(val1).firstEdge, len);

我仍在学习哈希表,所以放轻松。据我了解,您应该能够在哈希表中存储类的实例并检索它,firstEdge是我的类Vert的变量,所以理论上我应该在技术上能够毫无问题地检索它!

这是Vert

class Vert {
public Edge firstEdge;
public String name;

public Vert() {
firstEdge = null;

}

public Vert(String n)
{
firstEdge = null;
name = n;
}
}

最佳答案

您没有注释您的哈希表。所以ref.get(val1)返回 Object类,你正在尝试获取 firstEdge这不是 Object 的属性类(class)。您要么必须初始化哈希表,如 Hashtable<String, Vert> ref = new Hashtable<String, Vert>(10, 3);或投 ((Vert)ref.get(val1)).firstEdge

关于java - 在哈希表中存储和检索类的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25178835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com