{"id":4290,"date":"2014-03-30T09:43:32","date_gmt":"2014-03-30T09:43:32","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/casting-foreach-java-collection-of-common-programming-errors\/"},"modified":"2014-03-30T09:43:32","modified_gmt":"2014-03-30T09:43:32","slug":"casting-foreach-java-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2014\/03\/30\/casting-foreach-java-collection-of-common-programming-errors\/","title":{"rendered":"Casting &#8211; foreach &#8211; java-Collection of common programming errors"},"content":{"rendered":"<p>I have a one method like this:<\/p>\n<pre><code>private void myMethod(List myLists) {\n\n   for (Object val : myLists) {\n       val.getMyOtherMethod(); \/\/ for example\n   }\n\n}\n<\/code><\/pre>\n<p>How can I casting <code>val<\/code> to one of my objects?<\/p>\n<p>All my objects (list objects) contain same methods, like as <code>getMyOtherMethod()<\/code><\/p>\n<p>BR<\/p>\n<p><strong>edit: &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/strong><\/p>\n<p>I called myMethod few times, like as:<\/p>\n<pre><code>List var1;\nList var1;\nList var1;\n...\n...\n\nmyMethod(var1);\nmyMethod(var2);\nmyMethod(var3);\n<\/code><\/pre>\n<p>In this case, I do not know, witch subclass I send MySubclass1, MySubclass2 or MySubclass3. That is important for foreach loop.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have a one method like this: private void myMethod(List myLists) { for (Object val : myLists) { val.getMyOtherMethod(); \/\/ for example } } How can I casting val to one of my objects? All my objects (list objects) contain same methods, like as getMyOtherMethod() BR edit: &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; I called myMethod few times, like as: [&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-4290","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4290","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=4290"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/4290\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=4290"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=4290"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=4290"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}