0

I'm basically taking a big block of chars and using each suffix as a key, each key pointing an an ArrayList that contains the index where each of these suffixes can be found.

When I do a HashMap.get(suffix) it gives me the index of the key it added last, rather then the one I'm trying to pull (this occurs on the duplicates...

Here it is,

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
  • 1
    Indentation greatly improves readability Commented Oct 25, 2011 at 8:51

2 Answers 2

5

Seems to me like you're using the same ArrayList instance across your whole map. Perhaps instead of calling clear you should instantiate a new ArrayList when the suffix isn't in the map.

Sign up to request clarification or add additional context in comments.

Comments

2

You have only one ArrayList object that you modify and put in the map.

At the end, every key refers to the same list.

You need to create an array list for each key.

1 Comment

Hours of 'facepalming' and it was as easy as that! Thanks man!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.