{"id":99,"date":"2012-02-03T14:00:16","date_gmt":"2012-02-03T14:00:16","guid":{"rendered":"http:\/\/www.corrspt.com\/blog\/?p=99"},"modified":"2012-01-29T17:09:29","modified_gmt":"2012-01-29T17:09:29","slug":"avoiding-the-copy-paste-mistake-when-using-iterators-in-java","status":"publish","type":"post","link":"http:\/\/www.corrspt.com\/blog\/2012\/02\/03\/avoiding-the-copy-paste-mistake-when-using-iterators-in-java\/","title":{"rendered":"Avoiding the copy-paste mistake when using Iterators in Java"},"content":{"rendered":"<p>If you&#8217;re reading this it&#8217;s probably because you&#8217;ve done something like this (iterating over two lists in same block of code):<\/p>\n<pre class=\"brush:java\">\/\/Iterating with no problems\r\n\r\nIterator&lt;String&gt; it = list.iterator();  \r\n\r\nwhile (it.hasNext()){  \r\n\r\n     String current = it.next;  \r\n\r\n}  \r\n\r\n\/\/BUG!!!!!!\r\n\r\nIterator&lt;String&gt; it2 = list2.iterator();  \r\n\r\nwhile (it2.hasNext()){  \r\n\r\n     String current = it.next;  \/\/Not using the correct iterator\r\n\r\n}<\/pre>\n<p>Before anyone says anything, YES, YOU SHOULD USE FOREACH. But there are situations where you want access to the iterator (for instance, to remove an element using the iterator&#8217;s remove() method).<\/p>\n<p>This little bug where you increment the wrong iterator variable is a pain in the neck to figure out sometimes and I read this really interesting piece of advice in Joshua Bloch&#8217;s <em>Effective Java<\/em>. Instead of using a <em>while<\/em> loop, use a <em>for<\/em> loop like this:<\/p>\n<pre class=\"brush:java\">for (Iterator&lt;String&gt; it = list.iterator(); it.hasNext(); ){  \r\n\r\n   String current = it.next();  \r\n\r\n}<\/pre>\n<p>The snippet above allows you to reuse the &#8220;it&#8221; variable name as many times as you wish because, this time, <strong>the variable is declared inside the <em>for<\/em> loop<\/strong> thus, <strong>if you try to use it else where the compiler will generate an error (because the variable is only declared in the scope of the <em>for<\/em> loop) and you&#8217;ll avoid some debugging time <\/strong>\ud83d\ude42<\/p>\n<p>P.S &#8211; One could argue that you should iterate over each list inside separate methods, but the point of this post is to put the variable declaration in a narrower scope than the original idea (the one that creates the error in the first place).<\/p>\n<p>Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;re reading this it&#8217;s probably because you&#8217;ve done something like this (iterating over two lists in same block of code): \/\/Iterating with no problems Iterator&lt;String&gt; it = list.iterator(); while (it.hasNext()){ String current = it.next; } \/\/BUG!!!!!! Iterator&lt;String&gt; it2 = &hellip; <a href=\"http:\/\/www.corrspt.com\/blog\/2012\/02\/03\/avoiding-the-copy-paste-mistake-when-using-iterators-in-java\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[45,46],"_links":{"self":[{"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/posts\/99"}],"collection":[{"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":7,"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":125,"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/posts\/99\/revisions\/125"}],"wp:attachment":[{"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.corrspt.com\/blog\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}