我有一个包含一些HashMap类型的类:
private class XmlHandler extends DefaultHandler{
private HashMap<String, String> Attribs;
private HashMap<String, List<Attribs> >FragmentNode;
private HashMap<String, List<FragmentNode> > ActivityNode;
编译时,我收到此错误:
cannot find symbol FragmentNode and Attribs.
如果我修改插入原始类型的声明,即将HashMap<String, List<Attribs>
更改为HashMap<String, HashMap<String, String>>
编译器停止投诉。
我需要这样做吗? Java不了解像这样的类型声明吗?
答案 0 :(得分:1)
FragmentNode
和Attribs
不是类型,它是变量,您不能将变量指定为HashMap
的类型。
你可以这样做:
private HashMap<String, List<HashMap<String, String>>> FragmentNode;
或者您可以创建一个具有Attribs HashMap的类,如:
public class Attribs {
private HashMap<String, String> Attribs;
}
然后:
private HashMap<String, List<Attribs>> FragmentNode;
答案 1 :(得分:0)
好像你正在将类型与属性混合。
如果您想要这样的特定层次结构,则应定义新类:
class Attribs extends HashMap<String, String> {}
class FragmentNode extends HashMap<String, List<Attribs>> {}
class ActivityNode extends HashMap<String, List<FragmentNode>> {}
然后你可以创建这样的成员:
private class XmlHandler extends DefaultHandler {
private Attribs attribs;
private FragmentNode fragmentNode;
private ActivityNode activityNode;
}