{"id":1852,"date":"2022-08-30T15:19:59","date_gmt":"2022-08-30T15:19:59","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/12\/02\/indexoutofboundsexception-in-java-linkedlist-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:19:59","modified_gmt":"2022-08-30T15:19:59","slug":"indexoutofboundsexception-in-java-linkedlist-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/indexoutofboundsexception-in-java-linkedlist-collection-of-common-programming-errors\/","title":{"rendered":"IndexOutOfBoundsException in Java LinkedList-Collection of common programming errors"},"content":{"rendered":"<p>This method is throwing an IndexOutOfBoundsException, and I don&#8217;t understand why as I&#8217;ve guarded against it.<\/p>\n<pre><code>private static boolean firstLoop(int custNo, LinkedList stock, LinkedList custs, Random generator, String colour, int col, Tracking tracking) {\n\n    if ((stock.get(tracking.getLast(col)) != null) &amp;&amp; (stock.get(tracking.getLast(col)).getLength() &gt;= custs.get(custNo).getLength())) {\n\n        stock.get(tracking.getLast(col)).length = Cut.cut(stock.get(tracking.getLast(col)).getLength(), custs.get(custNo).getLength(), colour);\n        if (stock.get(tracking.getLast(col)).length &lt; 5) {\n\n            stock.remove(tracking.getLast(col)); \/\/**CAUSES EXCEPTION**\n\n            tracking.add();\/\/ recycle\n\n        }\n        return true;\n    } else {\n        for (int j = tracking.getLast(col) + 1; j &lt; stock.size(); j++) {\n\n            if ((stock.get(j).getLength() &gt;= custs.get(custNo).getLength())) {\n                \/\/ pipe is long enough, cut away the desired length\n                stock.get(j).setLength(Cut.cut(stock.get(j).getLength(), custs.get(custNo).getLength(), colour));\n\n                tracking.setLast(col, j);\n\n                return true;\n            }\n        }\n\n        \/\/ no suitable pipes available, order new one of correct colour with\n        \/\/ random length 100-200 then cut from it, add to arraylist\n        Pipe temp2 = new Pipe(col, generator.nextInt(101) + 100);\n        temp2.setLength(Cut.cut(temp2.length, custs.get(custNo).length, colour));\n        stock.add(temp2);\n        tracking.setLast(col, stock.size() - 1);\n        return false;\n    }\n\n}\n<\/code><\/pre>\n<p>I&#8217;ve found that the marked line is the one that causes the exception (the program runs perfectly when it&#8217;s commented out). However, I&#8217;m confused because tracking.getLast(col) works perfectly in the lines above it, and the remove function is not inside an iterator or a loop. Here is the Tracking class:<\/p>\n<pre><code>public class Tracking {\n\nstatic int lastR=0;\nstatic int lastG=0;\nstatic int lastY=0;\n\n\npublic void setLast(int col, int last){\n    if(col==0){\n        lastR=last;\n    }else if(col==1){\n        lastG=last;\n    }else if(col==2){\n        lastY=last;\n    }else{\n        System.out.println(\"Colour does not exist\");\n    }\n}\n\npublic static int getLast(int col){\n    if(col==0){\n        System.out.println(lastR+\" red\");\n        return lastR;\n    }else if(col==1){\n        System.out.println(lastG+\" green\");\n        return lastG;\n    }else{\n        System.out.println(lastY+\" yellow\");\n        return lastY;\n    }\n}\n<\/code><\/pre>\n<p>And this is the use of the method that throws the error:<\/p>\n<pre><code>if ((yellowStock.get(Tracking.getLast(2)) != null) &amp;&amp; (yellowStock.get(Tracking.getLast(2)).getLength() &gt;= custs.get(i).getLength())) {\n\n  firstLoop(i, yellowStock, custs, generator, \"yellow\", 2, recycle);\n} \n<\/code><\/pre>\n<p>Exception in thread &#8220;main&#8221; java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 at java.util.LinkedList.checkElementIndex(Unknown Source) at java.util.LinkedList.get(Unknown Source) at NextFit.next(NextFit.java:26) at Main.main(Main.java:58)<\/p>\n<p>Thank you very much guys.<\/p>\n<ol>\n<li>\n<p>The problem is as follows:<\/p>\n<p><code>LinkedList.remove()<\/code> comes in two flavours:<\/p>\n<ol>\n<li><code>LinkedList.remove(int index)<\/code>, which removes the element at the specified index<\/li>\n<li><code>LinkedList.remove(Object o)<\/code>, which removes the specifed element<\/li>\n<\/ol>\n<p>Further, <code>tracking.getLast(int col)<\/code> returns <code>int<\/code>, so when you call<\/p>\n<pre><code>stock.remove(tracking.getLast(col));\n<\/code><\/pre>\n<p>the version of <code>remove()<\/code> that is called is number 1, not number 2 as you wanted.<\/p>\n<p>Try calling <code>stock.remove(col);<\/code> or whatever makes sense.<\/p>\n<p>As an aside, your coding style could use some improvement, especially the use of static fields and methods in <code>Tracking<\/code> instead of <em>instance<\/em> fields and methods.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-12-02 21:01:23. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>This method is throwing an IndexOutOfBoundsException, and I don&#8217;t understand why as I&#8217;ve guarded against it. private static boolean firstLoop(int custNo, LinkedList stock, LinkedList custs, Random generator, String colour, int col, Tracking tracking) { if ((stock.get(tracking.getLast(col)) != null) &amp;&amp; (stock.get(tracking.getLast(col)).getLength() &gt;= custs.get(custNo).getLength())) { stock.get(tracking.getLast(col)).length = Cut.cut(stock.get(tracking.getLast(col)).getLength(), custs.get(custNo).getLength(), colour); if (stock.get(tracking.getLast(col)).length &lt; 5) { stock.remove(tracking.getLast(col)); \/\/**CAUSES [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1852","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1852","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1852"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1852\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1852"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1852"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1852"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}