2010-02-12 5 views
-4
진정한 코드
class{ 
    public HashMap<String, String> eachperson; 

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

(.. 난 그냥 내가 제대로 해시 맵을 가하고있어 알고 싶은 클래스와 기능의 앞에 공공/음부를 용서하십시오)이 Java 코드의 문제점은 무엇입니까? 왜 HashMap을 넣을 수 없습니까?

, 아래를 참조하십시오 :

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; 
    } 
} 
+5

무엇이 오류입니까? 그냥 "왜 내가 왜 안되니?"라고 말하지 마라. – gubby

+0

나는 안드로이드를하고 있기 때문에 나는이 오류를 알지 못한다. 그리고이 이상한 편집 소스 일을 팝업한다. – TIMEX

+0

이클립스 또는 뭐든간에 새 프로젝트를 만들고 컴파일러에서 무엇을 말하는지 확인하십시오. 추가 오류가 발생할 수 있지만 해시 맵에 대해 설명하는 코드를 찾으십시오. 아마도 런타임 환경 (아마도)과 관련이있을 수 있습니다. – ant

답변

5

받은 오류가 유용 할 수 있습니다. 그러나 첫 번째 추가에 대해 HashMap을 초기화하는 위치가 표시되지 않습니다. 할당없이 맨 위로 선언 한 다음 할당하지 않고 if(this.in_first_name) 대/소문자로 사용하려고합니다.

2

'eachperson'이라는 변수를 선언했습니다.이 변수는 HashMap 유형이지만 초기화하지 않았습니다. 또한 맵 구현을 변경해야 할 경우에 대비하여 맵 인터페이스를 사용하여 맵을 '사용'하는 것이 가장 좋습니다.

해시 맵 문제를 해결해야한다는 각자 선언을 바꿉니다.

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

'문자'요소를 얻은 후 HashMap을 '재설정'하는 코드가 있습니다. 문자는 startElement 다음에 발생하므로 처음으로 사용되기 전에지도가 초기화되지 않았습니다. 지도를 재생성하는 대신 '일반'을 사용하는 것이 좋습니다.

dset.eachperson.clear(); 

지도를 지우지 만 새 인스턴스를 만들 필요는 없습니다.

관련 문제