{"id":1723,"date":"2016-02-27T21:59:41","date_gmt":"2016-02-27T13:59:41","guid":{"rendered":"http:\/\/www.magicandlove.com\/blog\/?p=1723"},"modified":"2016-02-27T22:08:35","modified_gmt":"2016-02-27T14:08:35","slug":"looping-through-a-list-in-processing","status":"publish","type":"post","link":"http:\/\/www.magicandlove.com\/blog\/2016\/02\/27\/looping-through-a-list-in-processing\/","title":{"rendered":"Looping through a List in Processing"},"content":{"rendered":"<p>Here is a piece of demonstration code to use various ways to loop through a <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/List.html\">List<\/a> or <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> in Processing, i.e. Java. The first two examples use for loop and the third one uses the while loop.<br \/>\n&nbsp;<br \/>\nWe initialize the ArrayList with random integers.<\/p>\n<pre lang=\"java\">\r\nArrayList<Integer> iList = new ArrayList<Integer>();\r\niList.add(floor(random(10)));\r\niList.add(floor(random(10)));\r\niList.add(floor(random(10)));\r\n<\/pre>\n<p>The first method is the traditional way to loop with an index.<\/p>\n<pre lang='java\">\r\nfor (int i=0; i<iList.size(); i++) {\r\n    println(iList.get(i));\r\n}\r\n<\/pre>\n<p>The second method also uses the for loop, but with alternate syntax.<\/p>\n<pre lang=\"java\">\r\nfor (int i : iList) {\r\n    println(i);\r\n}\r\n<\/pre>\n<p>The third method uses the <a href=\"https:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/Iterator.html\">Iterator<\/a> through the ArrayList.<\/p>\n<pre lang=\"java\">\r\nIterator<Integer> it = iList.iterator();\r\nwhile (it.hasNext()) {\r\n    int i = it.next();\r\n    println(i);\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is a piece of demonstration code to use various ways to loop through a List or ArrayList in Processing, i.e. Java. The first two examples use for loop and the third one uses the while loop. &nbsp; We initialize the ArrayList with random integers. ArrayList iList = new ArrayList(); iList.add(floor(random(10))); iList.add(floor(random(10))); iList.add(floor(random(10))); The first [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[79,66],"tags":[145,62],"class_list":["post-1723","post","type-post","status-publish","format-standard","hentry","category-software-2","category-testing","tag-loop","tag-processing-org"],"_links":{"self":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/comments?post=1723"}],"version-history":[{"count":5,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1723\/revisions"}],"predecessor-version":[{"id":1728,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/posts\/1723\/revisions\/1728"}],"wp:attachment":[{"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/media?parent=1723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/categories?post=1723"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.magicandlove.com\/blog\/wp-json\/wp\/v2\/tags?post=1723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}