【问题标题】:What is wrong with this Java code? Why can't I put in HashMap? [closed]这段 Java 代码有什么问题?为什么我不能放入HashMap? [关闭]
【发布时间】:2010-02-12 12:06:36
【问题描述】:
class{
    public HashMap<String, String> eachperson;  

    apple(){
        this.eachperson.put("thekey","thevalue");
    }
}

(请原谅类和函数前面的公共/私有。我只是想知道我是否正确放置了哈希映射。)

真实代码见下图:

class ParsedDataSet{
    public HashMap<String, String> eachperson;      
    public List<HashMap<String,String>> peoplelist = new ArrayList<HashMap<String,String>>();
}

class ListprofileHandler extends DefaultHandler{
    private boolean in_results = true;
    private boolean in_item = false;
    private boolean in_first_name = false;
    private boolean in_last_name = false;
    private boolean in_picture_url = false;
    private boolean in_enditem_dummy = false;
    private ParsedDataSet dset = new ParsedDataSet();
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        if(localName.equals("results")){
            this.in_results = true;
        }else if(localName.equals("item")){
            this.in_item = true;
        }else if(localName.equals("first_name")){
            this.in_first_name = true;
        }else if(localName.equals("last_name")){
            this.in_last_name = true;
        }else if(localName.equals("picture_url")){
            this.in_picture_url = true;
        }else if(localName.equals("enditem_dummy")){
            this.in_enditem_dummy = true;
        }
    }
    public void endElement(String uri, String localName, String qName)throws SAXException {
        if(localName.equals("results")){
            this.in_results = false;
        }else if(localName.equals("item")){
            this.in_item = false;
        }else if(localName.equals("first_name")){
            this.in_first_name = false;
        }else if(localName.equals("last_name")){
            this.in_last_name = false;
        }else if(localName.equals("picture_url")){
            this.in_picture_url = false;
        }else if(localName.equals("enditem_dummy")){
            this.in_enditem_dummy = false;
        }
    }           
    public void characters(char ch[], int start, int length) throws SAXException {
        if(this.in_first_name){
            dset.eachperson.put("first_name", new String(ch, start, length));
        }
        if(this.in_enditem_dummy){
            dset.peoplelist.add(dset.eachperson);
            dset.eachperson = new HashMap<String,String>();  //Reached a new item. So reset the temporary hashmap.
        }

    }       

    public ParsedDataSet getParsedListData(){
        return this.dset;
    }   
}

【问题讨论】:

  • 错误是什么?不要只说“为什么我不能?”
  • 我不知道错误,因为我在做Android,它会弹出这个奇怪的编辑源的东西​​。
  • 在 Eclipse 或其他中创建一个新项目,看看编译器对此有何评论,您可能会遇到其他错误,但请查找描述您的 hashmap 内容的错误,可能与运行时环境有关(很可能)。
  • 将此标记为“Android”问题。并在 Eclipse 中检查您的 Logcat 窗口(在“DDMS”透视图中可用 - developer.android.com/intl/fr/guide/developing/eclipse-adt.html
  • 顺便说一句,你不需要每个人面前的“这个”。

标签: java hashmap


【解决方案1】:

您收到的具体错误会有所帮助。但是,我看不到您在哪里初始化 HashMap 以进行第一次添加。您在没有分配的情况下声明它,然后您尝试在 if(this.in_first_name) 的情况下使用它而不分配它。

【讨论】:

    【解决方案2】:

    您已经声明了一个名为“每个人”的变量,它的类型为 HashMap,但您从未初始化。此外,如果您需要更改地图实现,通常最好使用 Map 接口来“使用”地图。

    替换每个人声明应该解决 HashMap 问题。

    public HashMap<String,String> eachperson = new HashMap<String,String>(); 
    

    您确实有一些代码在 HashMap 获取“characters”元素后“重置”它。请注意,字符将出现在 startElement 之后,因此您的地图在第一次使用之前从未被初始化。您可能还想使用“清除”而不是重新创建地图。

    dset.eachperson.clear();
    

    这将清除您的地图,但不需要创建新实例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-10
      • 2011-03-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多