title: For-each loop variable holds value outside of ObservableList
body: <p>I have an <code>ObservableList&lt;String&gt;</code>, a <code>ListView</code> to hold it and an event to update the selected items to a <code>Label</code>.</p>
  <pre><code>ObservableList&lt;String&gt; items = FXCollections.observableArrayList(
              &quot;China&quot;, &quot;Japan&quot;, &quot;Korea&quot;, &quot;India&quot;);
  ListView&lt;String&gt; lvCountries = new ListView&lt;&gt;();
  lvCountries.setItems(items);
  lvCountries.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
  
  lvCountries.getSelectionModel().selectedIndexProperty().addListener(observable -&gt; {
      ObservableList&lt;Integer&gt; indices = lvCountries.getSelectionModel().getSelectedIndices();
      if (indices.size() != 0) {
          StringBuilder stringBuilder = new StringBuilder(&quot;Selected items are&quot;);
          indices.forEach(index -&gt; stringBuilder.append(&quot; &quot; + items.get(index)));
          lblSelected.setText(stringBuilder.toString());
      }
  });
  </code></pre>
  <p>Everything looks fine, but when I click randomly in high speed, sometimes it throws an <code>ArrayIndexOutOfBoundsException</code> because the <code>index</code> holds a value of <code>-1</code>. Again, this only happens <em>sometimes</em> when I click randomly quickly between items in MULTIPLE mode of the <code>ListView</code>.</p>
  <p>I'm not sure if it had something to do with the <code>ObservableList</code> or how the for-each loop works or the indices. The program doesn't crash or anything, but I would like to understand why and how this happens and the solution for it.</p>
  <p>I'm using Java 8 update 181 (build 1.8.0_181-b13), IntelliJ IDEA CE, MacOS 10.14</p>