Note that there are some explanatory texts on larger screens.

plurals
  1. POIterator removal/edit is safe, however I also need to edit all following Iterations
    primarykey
    data
    text
    <p>I have read a little about <code>ConcurrentModificationException</code> in stackflow and my actual update appears not to be the issue, it could be a problem in my design or I need a technique I haven't learnt yet.</p> <p>Example Situation: My iterator is running along position markers. Then an action can be performed to shift the markers over (e.g. Inserting into string). All Markers greater than the current position must also be shifted to preserve correctness.</p> <p>Task: How do I update the remaining markers without the iterator exploding? Can I refresh the iterator, or break and start the loop again?</p> <p>The following code is abstracted from my work.</p> <pre><code> public void innerLoop(Boolean b) { //An Example of what I'm working with HashMap&lt;String, HashSet&lt;Integer&gt;&gt; map = new HashMap&lt;String, HashSet&lt;Integer&gt;&gt;() { { put("Nonce", new HashSet&lt;Integer&gt;() { { add(1); add(2); add(3); add(4); add(5); } }); } }; //for each key for (String key: map.keySet()) { HashSet&lt;Integer&gt; positions = map.get(key); //for each integer for (Iterator&lt;Integer&gt; it = positions.iterator(); it.hasNext();) { Integer position = it.next(); System.out.println("position =" + position); //(out of scope) decision requiring elements from the outter loops if (new Random().nextBoolean()&amp;&amp;b) { //shift position by +4 (or whatever) //and every other (int &gt;= position) System.out.println("Shift " + position + " by 4"); Integer shift = 4; update(position, shift, positions); it.remove(); } } } } public void update(Integer current, Integer diff, Set&lt;Integer&gt; set) { if (set != null) { HashSet&lt;Integer&gt; temp = new HashSet&lt;Integer&gt;(); for (Integer old: set) { if (old &gt;= current) { temp.add(old); System.out.println(old + "Added to temp"); } } for (Integer old: temp) { set.remove(old); System.out.println(old + "removed"); set.add(old + diff); System.out.println((old + diff) + "Added"); } } } </code></pre> <hr> <p><strong>Edited with Garrett Hall Solution</strong></p> <pre><code> public void nestedloops() { HashMap&lt;String, HashSet&lt;Integer&gt;&gt; map = new HashMap&lt;String, HashSet&lt;Integer&gt;&gt;() { { put("Hello", new HashSet&lt;Integer&gt;() { { add(5); add(2); add(3); add(4); add(1); add(6); } }); } }; //for each key for (String key: map.keySet()) { ArrayList&lt;Integer&gt; positions = new ArrayList&lt;Integer&gt;(map.get(key)); //for each integer for (int i = 0; i &lt; positions.size(); i++) { Integer position = positions.get(i); System.out.println("[" + i + "] =" + position); //out of scope decision if (new Random().nextBoolean()) { //shift position by +4 //and every other (int &gt;= position) System.out.println("Shift after " + position + " by 4"); Integer shift = 4; //Update the array for (int j = 0; j &lt; positions.size(); j++) { Integer checkPosition = positions.get(j); if (checkPosition &gt; position) { System.out.println(checkPosition + "increased by 4"); positions.set(j, checkPosition + shift); } } } } //Add updated Array map.put(key, new HashSet&lt;Integer&gt;(positions)); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload