Sorry if this is a dumb question, I'm very new to working with maps.
(Removed most of original post because I completely misunderstood what I was supposed to do.)
static void learnFromText(Map<List<String>, List<String>> whatComesNext, List<String> text) {
List<String> maurice = new ArrayList<String>();
List <String> wallace = new ArrayList<String>();
List <String> romeo = new ArrayList<String>();
for (int i=0; i<=text.size()-3; i++) {
maurice.clear();
wallace.clear();
maurice.add(text.get(i));
maurice.add(text.get(i+1));
if (whatComesNext.containsKey(maurice)==false)
whatComesNext.put(maurice, romeo);
wallace=whatComesNext.get(maurice);
wallace.add(text.get(i+3));
whatComesNext.put(maurice, wallace);
}
}
I have a map of String list keys and String list values, where every key is two consecutive words in a String list "text". Every time I find the key "text.subList(i,i+2)" I need to add text.get(i+2) to the value of the key.
For example.
The elements of text are [A, B, C, D, A, B, E, F].
- The value of the key (A, B) is (C).
- The value of the key (B, C) is (D).
- The value of the key (C, D) is (E).
- The value of the key (D, A) is (B).
- The value of the key (A, B) is (C, E).
- The value of the key (B, E) is (F).
The problem with the code is that somehow it returns an ArrayIndexOutOfBoundsException for the size of the text when it should be constructing whatComesNext.