{"id":4455,"date":"2014-03-30T11:10:33","date_gmt":"2014-03-30T11:10:33","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-with-generics-collection-of-common-programming-errors\/"},"modified":"2014-03-30T11:10:33","modified_gmt":"2014-03-30T11:10:33","slug":"problem-with-generics-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/problem-with-generics-collection-of-common-programming-errors\/","title":{"rendered":"Problem with generics-Collection of common programming errors"},"content":{"rendered":"<p>The issue here is a little subtle. To make it correct, you&#8217;d want:<\/p>\n<pre><code>List myList = new ArrayList();\n<\/code><\/pre>\n<p>The definition of myList is asking for a <strong>List of Lists of Strings<\/strong>, but you are implementing it as an <strong>ArrayList of ArrayLists of Strings<\/strong>, and these don&#8217;t match. This would becoming an issue if later on you tried something like:<\/p>\n<pre><code>myList.add(new LinkedList());\n<\/code><\/pre>\n<p>This is ok by the definition of <strong>List of Lists of Strings<\/strong>, but violates the definition of the implementation. LinkedList <strong>is a List<\/strong>, but <strong>is not<\/strong> an ArrayList.<\/p>\n<p>If you want to enforce that it is an <strong>ArrayList of ArrayLists of Strings<\/strong>, then you can use the <code>ArrayList myList<\/code> definition, but it&#8217;s generally better to keep to interfaces.<\/p>\n<p>With my revised code, you can still make it in the <strong>implementation<\/strong> an ArrayList of ArrayLists of Strings.<\/p>\n<pre><code>List myList = new ArrayList();\nfor (int i = 0; i &lt; rowSize; i++)\n{\n myList.add(new ArrayList());\n}\n<\/code><\/pre>\n<p>It agrees with the definition of myList as an <strong>List of Lists of Strings<\/strong>, but you still reap the benefits of using an ArrayList behind the scenes.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The issue here is a little subtle. To make it correct, you&#8217;d want: List myList = new ArrayList(); The definition of myList is asking for a List of Lists of Strings, but you are implementing it as an ArrayList of ArrayLists of Strings, and these don&#8217;t match. This would becoming an issue if later on [&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-4455","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4455","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=4455"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4455\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4455"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4455"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4455"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}