-1

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].

  1. The value of the key (A, B) is (C).
  2. The value of the key (B, C) is (D).
  3. The value of the key (C, D) is (E).
  4. The value of the key (D, A) is (B).
  5. The value of the key (A, B) is (C, E).
  6. 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.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
user2709168
  • 117
  • 1
  • 14
  • 1
    I have downvoted this question because you have posted code on here without specifying what is wrong with it. We expect to see what you expect the code to do, why you expect it to do this, what it is actually doing, and why it is wrong. Please [edit] your question to include this information, and then I will consider retracting my downvote. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – Joe C Oct 31 '17 at 06:26
  • The problem with the code is that somehow it returns an ArrayIndexOutOfBoundsException for the size of the text when it should be constructing whatComesNext. – user2709168 Oct 31 '17 at 06:28
  • Am I allowed to update and keep asking here? I read the attached thread and realized a small error in the code. I've eliminated the OutOfBoundsException and now the problem is that the code sets every value to null. – user2709168 Oct 31 '17 at 06:34
  • Do not edit your question in a manner that completely changes the question. Thus is not an interactive help site. – Raedwald Oct 31 '17 at 07:51
  • Sorry, I'm genuinely trying to understand how to use this site properly. If I fix a small error in my code to solve a problem that returns a different problem, am I supposed to ask an entirely new question? I didn't feel that I completely changed the question, I fixed a typo that now returned unintended results. – user2709168 Oct 31 '17 at 17:30
  • If you have an entirely different problem, you should ask a new question. That said, you should spend some serious time attempting to debug your problem first. The last thing you want is multiple questions which are closed because of small errors such as this, as it may make it more difficult to ask further questions. – Joe C Oct 31 '17 at 21:04

1 Answers1

1

The problem is that clear is not used for you to recreate the List. Instead, in your code, you are using the same instance of List in your every element of the Map. Then when you execute clear, it is in fact clearing all your elements in your map.

A minimum modification of your code should be as below.

static void learnFromText(Map<List<String>, List<String>> whatComesNext, List<String> text) {
    List<String> maurice;
    List <String> wallace;
    for (int i=0; i<=text.size()-3; i++) {
      maurice = new ArrayList<>();
      maurice.add(text.get(i));
      maurice.add(text.get(i+1));
      if (whatComesNext.containsKey(maurice)==false)
        whatComesNext.put(maurice, new ArrayList<>());
      wallace=whatComesNext.get(maurice);
      wallace.add(text.get(i+2));
      whatComesNext.put(maurice, wallace);
    }
}
Alex
  • 803
  • 4
  • 9
  • So you're saying that by initializing maurice as a new ArrayList, every key dynamically references it so that clearing it clears my keys as well? But by not explicitly initializing maurice, it creates a new, separate List following every time I clear it? – user2709168 Oct 31 '17 at 17:37
  • You're right, keys and values need to be with different instances. I overlooked that and thought `maurice` is only used for calling `containsKey`. I have edited also to make `mauirce` a new instance for each loop. – Alex Nov 01 '17 at 00:39