{"id":210,"date":"2024-07-30T06:52:39","date_gmt":"2024-07-30T06:52:39","guid":{"rendered":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/chapter\/iterator-examples-2\/"},"modified":"2026-03-16T14:30:44","modified_gmt":"2026-03-16T14:30:44","slug":"iterator-examples-2","status":"publish","type":"chapter","link":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/chapter\/iterator-examples-2\/","title":{"raw":"Iterator Examples","rendered":"Iterator Examples"},"content":{"raw":"<div class=\"iterator-examples\">\n<h3>Iterating over a Collection with an Iterator<\/h3>\n<p class=\"import-Normal\">For the examples that follow, we will use a list of <em>Player <\/em>objects, where a <em>Player <\/em>has a <em>name <\/em>and a <em>score<\/em>.<\/p>\n\n<pre class=\"import-Normal\">ArrayList&lt;Player&gt; players = <strong>new<\/strong> ArrayList&lt;Player&gt;();\nplayers.add(<strong>new<\/strong> Player(\"Pam\", 24));\nplayers.add(<strong>new<\/strong> Player(\"Len\", 19));\nplayers.add(<strong>new<\/strong> Player(\"Malia\", 37));\nplayers.add(<strong>new<\/strong> Player(\"Bob\", 13));\nplayers.add(<strong>new<\/strong> Player(\"Rea\", 46));\n\n\n<\/pre>\n<p class=\"import-Normal\">To use an iterator to traverse this list:<\/p>\n\n<\/div>\n<pre>Iterator&lt;Player&gt; iter = players.iterator();\nwhile(iter.hasNext()) {\n   Player player = iter.next();\n    System.out.println(player);\n}\n<\/pre>\n&nbsp;\n<div class=\"iterator-examples\">\n<p class=\"import-Normal\">Note that the <em>Iterator <\/em>interface is generic, so we must specify what type of objects we are iterating over. The result of the code above is of course, no different from either of the approaches below:<\/p>\n\n<div style=\"text-align: left\">\n<table style=\"width: 438.75pt;height: 169px\">\n<tbody>\n<tr class=\"Table1-R\" style=\"height: 14.25pt\">\n<td class=\"Table1-C\" style=\"border-width: 0pt 0pt 1pt;border-style: none none solid;border-color: #000000;padding: 0pt 5pt;height: 30px;width: 238.833px\">\n<p class=\"import-Normal\"><strong>for-each Loop<\/strong><\/p>\n<\/td>\n<td class=\"Table1-C\" style=\"padding: 0pt 5pt;border: 0pt #000000;height: 30px;width: 2.98333px\">\n<p class=\"import-Normal\"><\/p>\n<\/td>\n<td class=\"Table1-C\" style=\"border-width: 0pt 0pt 1pt;border-style: none none solid;border-color: #000000;padding: 0pt 5pt;height: 30px;width: 301.717px\">\n<p class=\"import-Normal\"><strong>Indexed Loop<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr class=\"Table1-R\" style=\"height: 59.25pt\">\n<td class=\"Table1-C\" style=\"padding: 0pt 5pt;border: 0pt #000000;height: 129px;width: 238.833px\">\n<p class=\"import-Normal\"><code><strong>for<\/strong>(Player player : players) {<\/code><\/p>\n<p class=\"import-Normal\"><code>\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(player);<\/code><\/p>\n<p class=\"import-Normal\"><code>}<\/code><\/p>\n<p class=\"import-Normal\"><\/p>\n<\/td>\n<td class=\"Table1-C\" style=\"padding: 0pt 5pt;border: 0pt #000000;height: 129px;width: 2.98333px\">\n<p class=\"import-Normal\"><\/p>\n<\/td>\n<td class=\"Table1-C\" style=\"padding: 0pt 5pt;border: 0pt #000000;height: 129px;width: 301.717px\">\n<p class=\"import-Normal\"><code><strong>for<\/strong>(<strong>int<\/strong> i = 0; i &lt; players.size(); i++) {<\/code><\/p>\n<p class=\"import-Normal\"><code>\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(players.get(i));<\/code><\/p>\n<p class=\"import-Normal\"><code>}<\/code><\/p>\n<p class=\"import-Normal\"><\/p>\n<\/td>\n<\/tr>\n<tr style=\"height: 10px\">\n<td style=\"height: 10px;width: 239.2px\"><\/td>\n<td style=\"height: 10px;width: 3.35px\"><\/td>\n<td style=\"height: 10px;width: 302.083px\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p class=\"import-Normal\">Most people would not use an iterator to iterator over a collection, they would use the <em>for-each <\/em>or indexed loop. A good use for an iterator is to remove certain elements as we iterate over it, which we consider in the next section. Interestingly, the <em>for-each <\/em>loop compiles down to an iterator.<\/p>\n<p class=\"import-Normal\">The <em>Iterator <\/em>interface is an excellent example of object-oriented design, which uses information hiding. An iterator hides the actual storage mechanism of the data. Thus, the user of an iterator does not need to know how the data is stored, it just knows that it can access the <em>next<\/em> item. For example, with the indexed loop, if you are using an <em>ArrayList<\/em>, you must use the <em>get <\/em>method, with other types of collections, there may be some other method to access an item, or no method at all. With an iterator, you don\u2019t need to know these.<\/p>\n\n<h3>Filtering a Collection with an Iterator \u2013 Removing Elements<\/h3>\n<p class=\"import-Normal\"><em>Filtering <\/em>a collection refers to the idea of finding all the elements in a collection that meet certain criteria. The way we consider it here, is we will remove all elements that meet the criteria from the collection. An <em>Iterator <\/em>is the preferred way to filter a collection by removing elements.<\/p>\n<p class=\"import-Normal\">In the <em>players <\/em>list considered above, suppose we want to remove all players whose score is less than 20. In this case, we can use the <em>Iterator<\/em>\u2019s <em>remove <\/em>method:<\/p>\n\n<\/div>\n<pre>Iterator&lt;Player&gt; iter = players.iterator();\nwhile(iter.hasNext() ) {\n   Player player = iter.next();\n   if(player.getScore() &lt; 20) {\n      \titer.remove();\n   }\n}\n<\/pre>\n&nbsp;\n<div class=\"iterator-examples\">\n<p class=\"import-Normal\">A common mistake is to use a <em>for-each <\/em>loop and then using the collection\u2019s <em>remove <\/em>method. However, such code will fail if the <em>remove <\/em>method is executed, throwing a <em>ConcurrentModificationException.<\/em> You cannot modify a collection (add to or remove from) while iterating over it with a <em>for-each <\/em>loop. Another common mistake is to use an indexed loop, somewhat naively. An example is found in an Appendix. Other approaches to filtering that work are: an indexed loop with a subtle modification of the index inside the loop (bad practice), a <em>while <\/em>loop, or using an indexed loop traversing the list in reverse order.<\/p>\n\n<h3>Filtering a Collection with an Iterator \u2013 Removing &amp; Returning Elements<\/h3>\n<p class=\"import-Normal\">Suppose you want to remove certain elements from a collection and also return the removed elements in a new collection. Considering the example from above, suppose we want to (a) remove all players whose score is less than 20 from the <em>players <\/em>list and (b) put those removed players in another list named <em>lowScorePlayers. <\/em>Note that every time the <em>next <\/em>method is called, the next element in the collection is retrieved. Thus, if you call <em>next <\/em>twice inside the loop, you will receive the next two elements, respectively. Thus, if you need the next element more than once in the loop, you must store it in a variable. The correct version is shown in the table below on the left. There, we make one call to <em>next <\/em>inside the loop, capturing the item in the <em>player <\/em>variable. Then, <em>player <\/em>is used twice. Both examples define this list to store the players that are to be stored in a separate list:<\/p>\n<p class=\"import-Normal\" style=\"margin-left: 18pt;text-indent: 0pt\">ArrayList&lt;Player&gt; lowScorePlayers = <strong>new<\/strong> ArrayList&lt;Player&gt;();<\/p>\n\n<div style=\"text-align: left\">\n<table style=\"width: 438pt\">\n<tbody>\n<tr class=\"Table2-R\" style=\"height: 15pt\">\n<td class=\"Table2-C\" style=\"padding: 0pt 5pt 0pt 5pt;border: solid #000000 1pt\">\n<p class=\"import-Normal\"><strong>Correct<\/strong><\/p>\n<\/td>\n<td class=\"Table2-C\" style=\"border-top: solid #000000 1pt;border-right: solid #000000 1pt;border-bottom: solid #000000 1pt;border-left: solid #000000 0.75pt;padding: 0pt 5pt 0pt 5pt\">\n<p class=\"import-Normal\"><strong>Incorrect<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr class=\"Table2-R\" style=\"height: 304.5pt\">\n<td class=\"Table2-C\" style=\"border-top: solid #000000 0.75pt;border-right: solid #000000 1pt;border-bottom: solid #000000 1pt;border-left: solid #000000 1pt;padding: 0pt 5pt 0pt 5pt\">\n<p class=\"import-Normal\"><code>Iterator&lt;Player&gt; iter = players.iterator();<\/code><\/p>\n<p class=\"import-Normal\"><code><strong>while<\/strong>( iter.hasNext() ) {<\/code><\/p>\n<p class=\"import-Normal\"><code>\u00a0\u00a0\u00a0 Player player = iter.next();<\/code><\/p>\n<p class=\"import-Normal\"><code><strong>\u00a0\u00a0\u00a0 if<\/strong>(player.getScore() &lt; 20) {<\/code><\/p>\n<p class=\"import-Normal\"><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 iter.remove();<\/code><\/p>\n<p class=\"import-Normal\"><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lowScorePlayers.add(player);<\/code><\/p>\n<p class=\"import-Normal\"><code>\u00a0\u00a0\u00a0 }<\/code><\/p>\n<p class=\"import-Normal\"><code>}<\/code><\/p>\n<p class=\"import-Normal\"><code>\/\/ Result<\/code><\/p>\n<p class=\"import-Normal\"><code>\/\/ players: [Pam-24, Malia-37, Rea-46]<\/code><\/p>\n<p class=\"import-Normal\"><code>\/\/lowScorePlayers: [Len-19, Bob-13]<\/code><\/p>\n<\/td>\n<td class=\"Table2-C\" style=\"border-top: solid #000000 0.75pt;border-right: solid #000000 1pt;border-bottom: solid #000000 1pt;border-left: solid #000000 0.75pt;padding: 0pt 5pt 0pt 5pt\">\n<p class=\"import-Normal\"><code>Iterator&lt;Player&gt; iter = players.iterator();<\/code><\/p>\n<p class=\"import-Normal\"><code><strong>while<\/strong>(iter.hasNext()) {<\/code><\/p>\n<p class=\"import-Normal\"><code><strong>\u00a0\u00a0\u00a0 if<\/strong>(<span style=\"background-color: #ffff00\">iter.next().<\/span>getScore() &lt; 20) {<\/code><\/p>\n<p class=\"import-Normal\" style=\"margin-left: 53pt;text-indent: 0pt\"><code>iter.remove();<\/code><\/p>\n<p class=\"import-Normal\" style=\"margin-left: 53pt;text-indent: 0pt\"><code>lowScorePlayers.add<span style=\"background-color: #ffff00\">(iter.next())<\/span>;<\/code><\/p>\n<p class=\"import-Normal\"><code>\u00a0\u00a0\u00a0 }<\/code><\/p>\n<p class=\"import-Normal\"><code>}<\/code><\/p>\n<p class=\"import-Normal\"><code>\/\/ Result<\/code><\/p>\n<p class=\"import-Normal\"><code>\/\/ players: [Pam-24, Malia-37, Rea-46]<\/code><\/p>\n<p class=\"import-Normal\"><code>\/\/ lowScorePlayers: [Malia-37, Rea-46]<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p class=\"import-Normal\">For the incorrect version, consider the original list of players:<\/p>\n<p class=\"import-Normal\" style=\"margin-left: 18pt;text-indent: 0pt\">[Pam-24, Len-19, Malia-37, Bob-13, Rea-46]<\/p>\n<p class=\"import-Normal\">When the player, \u201cLen-19\u201d is found, it is removed, but then the subsequent call to <em>iter.next() <\/em>advances to the next player, \u201cMalia-13\u201d, which is added to <em>lowScorePlayers. <\/em>Then, the loop repeats, where the first <em>next <\/em>retrieves: \u201cBob-13, which is removed and the subsequent call to <em>iter.next() <\/em>adds \u201cRea-46\u201d to <em>lowScorePlayers<\/em>. So, we can see that (a) we are putting the wrong players in the list, (b) some players do not have their score checked \u2013 we are effectively skipping them, (c) if there has been a player with score less than 20 in the last space, then there is the possibility that the code would throw an exception as the coded tried to add the next element to <em>lowScorePlayers, <\/em>(d) the loop, in this case only executed 3 times. This is a common mistake. Thus, if you need the current element in the loop more than once, you should store it in a variable with a single call to <em>iter.next<\/em> as shown in the correct version above.<\/p>\n\n<\/div>","rendered":"<div class=\"iterator-examples\">\n<h3>Iterating over a Collection with an Iterator<\/h3>\n<p class=\"import-Normal\">For the examples that follow, we will use a list of <em>Player <\/em>objects, where a <em>Player <\/em>has a <em>name <\/em>and a <em>score<\/em>.<\/p>\n<pre class=\"import-Normal\">ArrayList&lt;Player&gt; players = <strong>new<\/strong> ArrayList&lt;Player&gt;();\nplayers.add(<strong>new<\/strong> Player(\"Pam\", 24));\nplayers.add(<strong>new<\/strong> Player(\"Len\", 19));\nplayers.add(<strong>new<\/strong> Player(\"Malia\", 37));\nplayers.add(<strong>new<\/strong> Player(\"Bob\", 13));\nplayers.add(<strong>new<\/strong> Player(\"Rea\", 46));\n\n\n<\/pre>\n<p class=\"import-Normal\">To use an iterator to traverse this list:<\/p>\n<\/div>\n<pre>Iterator&lt;Player&gt; iter = players.iterator();\nwhile(iter.hasNext()) {\n   Player player = iter.next();\n    System.out.println(player);\n}\n<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"iterator-examples\">\n<p class=\"import-Normal\">Note that the <em>Iterator <\/em>interface is generic, so we must specify what type of objects we are iterating over. The result of the code above is of course, no different from either of the approaches below:<\/p>\n<div style=\"text-align: left\">\n<table style=\"width: 438.75pt;height: 169px\">\n<tbody>\n<tr class=\"Table1-R\" style=\"height: 14.25pt\">\n<td class=\"Table1-C\" style=\"border-width: 0pt 0pt 1pt;border-style: none none solid;border-color: #000000;padding: 0pt 5pt;height: 30px;width: 238.833px\">\n<p class=\"import-Normal\"><strong>for-each Loop<\/strong><\/p>\n<\/td>\n<td class=\"Table1-C\" style=\"padding: 0pt 5pt;border: 0pt #000000;height: 30px;width: 2.98333px\">\n<p class=\"import-Normal\">\n<\/td>\n<td class=\"Table1-C\" style=\"border-width: 0pt 0pt 1pt;border-style: none none solid;border-color: #000000;padding: 0pt 5pt;height: 30px;width: 301.717px\">\n<p class=\"import-Normal\"><strong>Indexed Loop<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr class=\"Table1-R\" style=\"height: 59.25pt\">\n<td class=\"Table1-C\" style=\"padding: 0pt 5pt;border: 0pt #000000;height: 129px;width: 238.833px\">\n<p class=\"import-Normal\"><code><strong>for<\/strong>(Player player : players) {<\/code><\/p>\n<p class=\"import-Normal\"><code>\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(player);<\/code><\/p>\n<p class=\"import-Normal\"><code>}<\/code><\/p>\n<p class=\"import-Normal\">\n<\/td>\n<td class=\"Table1-C\" style=\"padding: 0pt 5pt;border: 0pt #000000;height: 129px;width: 2.98333px\">\n<p class=\"import-Normal\">\n<\/td>\n<td class=\"Table1-C\" style=\"padding: 0pt 5pt;border: 0pt #000000;height: 129px;width: 301.717px\">\n<p class=\"import-Normal\"><code><strong>for<\/strong>(<strong>int<\/strong> i = 0; i &lt; players.size(); i++) {<\/code><\/p>\n<p class=\"import-Normal\"><code>\u00a0\u00a0 System.<strong><em>out<\/em><\/strong>.println(players.get(i));<\/code><\/p>\n<p class=\"import-Normal\"><code>}<\/code><\/p>\n<p class=\"import-Normal\">\n<\/td>\n<\/tr>\n<tr style=\"height: 10px\">\n<td style=\"height: 10px;width: 239.2px\"><\/td>\n<td style=\"height: 10px;width: 3.35px\"><\/td>\n<td style=\"height: 10px;width: 302.083px\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p class=\"import-Normal\">Most people would not use an iterator to iterator over a collection, they would use the <em>for-each <\/em>or indexed loop. A good use for an iterator is to remove certain elements as we iterate over it, which we consider in the next section. Interestingly, the <em>for-each <\/em>loop compiles down to an iterator.<\/p>\n<p class=\"import-Normal\">The <em>Iterator <\/em>interface is an excellent example of object-oriented design, which uses information hiding. An iterator hides the actual storage mechanism of the data. Thus, the user of an iterator does not need to know how the data is stored, it just knows that it can access the <em>next<\/em> item. For example, with the indexed loop, if you are using an <em>ArrayList<\/em>, you must use the <em>get <\/em>method, with other types of collections, there may be some other method to access an item, or no method at all. With an iterator, you don\u2019t need to know these.<\/p>\n<h3>Filtering a Collection with an Iterator \u2013 Removing Elements<\/h3>\n<p class=\"import-Normal\"><em>Filtering <\/em>a collection refers to the idea of finding all the elements in a collection that meet certain criteria. The way we consider it here, is we will remove all elements that meet the criteria from the collection. An <em>Iterator <\/em>is the preferred way to filter a collection by removing elements.<\/p>\n<p class=\"import-Normal\">In the <em>players <\/em>list considered above, suppose we want to remove all players whose score is less than 20. In this case, we can use the <em>Iterator<\/em>\u2019s <em>remove <\/em>method:<\/p>\n<\/div>\n<pre>Iterator&lt;Player&gt; iter = players.iterator();\nwhile(iter.hasNext() ) {\n   Player player = iter.next();\n   if(player.getScore() &lt; 20) {\n      \titer.remove();\n   }\n}\n<\/pre>\n<p>&nbsp;<\/p>\n<div class=\"iterator-examples\">\n<p class=\"import-Normal\">A common mistake is to use a <em>for-each <\/em>loop and then using the collection\u2019s <em>remove <\/em>method. However, such code will fail if the <em>remove <\/em>method is executed, throwing a <em>ConcurrentModificationException.<\/em> You cannot modify a collection (add to or remove from) while iterating over it with a <em>for-each <\/em>loop. Another common mistake is to use an indexed loop, somewhat naively. An example is found in an Appendix. Other approaches to filtering that work are: an indexed loop with a subtle modification of the index inside the loop (bad practice), a <em>while <\/em>loop, or using an indexed loop traversing the list in reverse order.<\/p>\n<h3>Filtering a Collection with an Iterator \u2013 Removing &amp; Returning Elements<\/h3>\n<p class=\"import-Normal\">Suppose you want to remove certain elements from a collection and also return the removed elements in a new collection. Considering the example from above, suppose we want to (a) remove all players whose score is less than 20 from the <em>players <\/em>list and (b) put those removed players in another list named <em>lowScorePlayers. <\/em>Note that every time the <em>next <\/em>method is called, the next element in the collection is retrieved. Thus, if you call <em>next <\/em>twice inside the loop, you will receive the next two elements, respectively. Thus, if you need the next element more than once in the loop, you must store it in a variable. The correct version is shown in the table below on the left. There, we make one call to <em>next <\/em>inside the loop, capturing the item in the <em>player <\/em>variable. Then, <em>player <\/em>is used twice. Both examples define this list to store the players that are to be stored in a separate list:<\/p>\n<p class=\"import-Normal\" style=\"margin-left: 18pt;text-indent: 0pt\">ArrayList&lt;Player&gt; lowScorePlayers = <strong>new<\/strong> ArrayList&lt;Player&gt;();<\/p>\n<div style=\"text-align: left\">\n<table style=\"width: 438pt\">\n<tbody>\n<tr class=\"Table2-R\" style=\"height: 15pt\">\n<td class=\"Table2-C\" style=\"padding: 0pt 5pt 0pt 5pt;border: solid #000000 1pt\">\n<p class=\"import-Normal\"><strong>Correct<\/strong><\/p>\n<\/td>\n<td class=\"Table2-C\" style=\"border-top: solid #000000 1pt;border-right: solid #000000 1pt;border-bottom: solid #000000 1pt;border-left: solid #000000 0.75pt;padding: 0pt 5pt 0pt 5pt\">\n<p class=\"import-Normal\"><strong>Incorrect<\/strong><\/p>\n<\/td>\n<\/tr>\n<tr class=\"Table2-R\" style=\"height: 304.5pt\">\n<td class=\"Table2-C\" style=\"border-top: solid #000000 0.75pt;border-right: solid #000000 1pt;border-bottom: solid #000000 1pt;border-left: solid #000000 1pt;padding: 0pt 5pt 0pt 5pt\">\n<p class=\"import-Normal\"><code>Iterator&lt;Player&gt; iter = players.iterator();<\/code><\/p>\n<p class=\"import-Normal\"><code><strong>while<\/strong>( iter.hasNext() ) {<\/code><\/p>\n<p class=\"import-Normal\"><code>\u00a0\u00a0\u00a0 Player player = iter.next();<\/code><\/p>\n<p class=\"import-Normal\"><code><strong>\u00a0\u00a0\u00a0 if<\/strong>(player.getScore() &lt; 20) {<\/code><\/p>\n<p class=\"import-Normal\"><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 iter.remove();<\/code><\/p>\n<p class=\"import-Normal\"><code>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 lowScorePlayers.add(player);<\/code><\/p>\n<p class=\"import-Normal\"><code>\u00a0\u00a0\u00a0 }<\/code><\/p>\n<p class=\"import-Normal\"><code>}<\/code><\/p>\n<p class=\"import-Normal\"><code>\/\/ Result<\/code><\/p>\n<p class=\"import-Normal\"><code>\/\/ players: [Pam-24, Malia-37, Rea-46]<\/code><\/p>\n<p class=\"import-Normal\"><code>\/\/lowScorePlayers: [Len-19, Bob-13]<\/code><\/p>\n<\/td>\n<td class=\"Table2-C\" style=\"border-top: solid #000000 0.75pt;border-right: solid #000000 1pt;border-bottom: solid #000000 1pt;border-left: solid #000000 0.75pt;padding: 0pt 5pt 0pt 5pt\">\n<p class=\"import-Normal\"><code>Iterator&lt;Player&gt; iter = players.iterator();<\/code><\/p>\n<p class=\"import-Normal\"><code><strong>while<\/strong>(iter.hasNext()) {<\/code><\/p>\n<p class=\"import-Normal\"><code><strong>\u00a0\u00a0\u00a0 if<\/strong>(<span style=\"background-color: #ffff00\">iter.next().<\/span>getScore() &lt; 20) {<\/code><\/p>\n<p class=\"import-Normal\" style=\"margin-left: 53pt;text-indent: 0pt\"><code>iter.remove();<\/code><\/p>\n<p class=\"import-Normal\" style=\"margin-left: 53pt;text-indent: 0pt\"><code>lowScorePlayers.add<span style=\"background-color: #ffff00\">(iter.next())<\/span>;<\/code><\/p>\n<p class=\"import-Normal\"><code>\u00a0\u00a0\u00a0 }<\/code><\/p>\n<p class=\"import-Normal\"><code>}<\/code><\/p>\n<p class=\"import-Normal\"><code>\/\/ Result<\/code><\/p>\n<p class=\"import-Normal\"><code>\/\/ players: [Pam-24, Malia-37, Rea-46]<\/code><\/p>\n<p class=\"import-Normal\"><code>\/\/ lowScorePlayers: [Malia-37, Rea-46]<\/code><\/p>\n<\/td>\n<\/tr>\n<tr>\n<td><\/td>\n<td><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<p class=\"import-Normal\">For the incorrect version, consider the original list of players:<\/p>\n<p class=\"import-Normal\" style=\"margin-left: 18pt;text-indent: 0pt\">[Pam-24, Len-19, Malia-37, Bob-13, Rea-46]<\/p>\n<p class=\"import-Normal\">When the player, \u201cLen-19\u201d is found, it is removed, but then the subsequent call to <em>iter.next() <\/em>advances to the next player, \u201cMalia-13\u201d, which is added to <em>lowScorePlayers. <\/em>Then, the loop repeats, where the first <em>next <\/em>retrieves: \u201cBob-13, which is removed and the subsequent call to <em>iter.next() <\/em>adds \u201cRea-46\u201d to <em>lowScorePlayers<\/em>. So, we can see that (a) we are putting the wrong players in the list, (b) some players do not have their score checked \u2013 we are effectively skipping them, (c) if there has been a player with score less than 20 in the last space, then there is the possibility that the code would throw an exception as the coded tried to add the next element to <em>lowScorePlayers, <\/em>(d) the loop, in this case only executed 3 times. This is a common mistake. Thus, if you need the current element in the loop more than once, you should store it in a variable with a single call to <em>iter.next<\/em> as shown in the correct version above.<\/p>\n<\/div>\n","protected":false},"author":1,"menu_order":2,"template":"","meta":{"pb_show_title":"","pb_short_title":"","pb_subtitle":"","pb_authors":[],"pb_section_license":""},"chapter-type":[49],"contributor":[],"license":[],"class_list":["post-210","chapter","type-chapter","status-publish","hentry","chapter-type-numberless"],"part":207,"_links":{"self":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapters\/210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapters"}],"about":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/wp\/v2\/types\/chapter"}],"author":[{"embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/wp\/v2\/users\/1"}],"version-history":[{"count":1,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapters\/210\/revisions"}],"predecessor-version":[{"id":211,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapters\/210\/revisions\/211"}],"part":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/parts\/207"}],"metadata":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapters\/210\/metadata\/"}],"wp:attachment":[{"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/wp\/v2\/media?parent=210"}],"wp:term":[{"taxonomy":"chapter-type","embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/pressbooks\/v2\/chapter-type?post=210"},{"taxonomy":"contributor","embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/wp\/v2\/contributor?post=210"},{"taxonomy":"license","embeddable":true,"href":"https:\/\/libraryresources.nse.org.ng\/computersciencetwo\/wp-json\/wp\/v2\/license?post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}