2011-10-25 1 views
0

기본적으로 큰 문자 블록을 사용하고 각 접미사를 키로 사용하며 각 키는 색인이 들어있는 ArrayList를 가리키고 있습니다. 접미사를 찾을 수 있습니다. 나는 HashMap.get (접미사) 할 그것이 나에게이 마지막으로 추가 된 키의 인덱스를 줄 때내 HashMap <String, ArrayList> 키가 잘못된 값을 얻고 있습니다.

, 오히려 그때 당겨 노력하고있어 하나 (이 여기에

... 중복 발생 그것은 당신이 당신의 전체지도에서 동일한 ArrayList 인스턴스를 사용. 아마도 대신 접미사가지도에없는 경우 새 ArrayList를 인스턴스화해야 clear를 호출하는 것처럼

protected HashMap<String, ArrayList<Integer>> makeMap(char[] textStored) 
{ 
    HashMap<String, ArrayList<Integer>> phraseMap = 
      new HashMap<String, ArrayList<Integer>>(); 
    ArrayList<Integer> indexList = new ArrayList<Integer>(); 
    Boolean word = true; 
    String suffix; 
    int wordEndIndex = 0; 
    for (int i = 0; i < textStored.length; i++) { 
     word &= Character.isLetter(textStored[i]); 
     if (word) { 
      for (int h = i + 1;h < textStored.length;h++) { 
       if (!Character.isLetter(textStored[h])) { 
        wordEndIndex = h; 
        break; 
       } 
      }//PULLING THE NEXT SUFFIX vvv 
      //This finds the next word/suffix: 
      suffix = new String(textStored).substring(i,wordEndIndex + 1); 

      //if my hashmap already contains this key: 
      if (phraseMap.containsKey(suffix)) { 
       System.out.println(suffix); 
       indexList = phraseMap.get(suffix); 
       System.out.println(indexList);// This is printing 
        // the wrong info, 
        // telling me my phraseMap.get(suffix) is 
        // using the wrong key, yet 
        // I'm printing out the suffix 
        // directly before that line, 
        // and I'm definitatly inputting 
        // the correct suffix... 
       indexList.add(i); 
       phraseMap.put(suffix,indexList); 
       System.out.println(indexList); 
      } else { 
       // System.out.println(suffix); 
       indexList.clear(); 
       indexList.add(i); 
       phraseMap.put(suffix, indexList); 
       // System.out.println(phraseMap.get(suffix)); 
      } 

     } 
     word = !Character.isLetter(textStored[i]); 
    } 

    return phraseMap; 
} 
+1

들여 쓰기가 크게 향상되었습니다. – michael667

답변

5

나에게 보인다이다.

2

맵에 수정하여 넣은 ArrayList 객체는 하나뿐입니다.

결국 모든 키는 동일한 목록을 참조합니다.

각 키에 대한 배열 목록을 만들어야합니다.

+0

'facepalming'의 시간은 그만큼 쉬웠습니다! 고마워요! – ChrisB92

관련 문제