{"id":134,"date":"2020-03-04T06:29:00","date_gmt":"2020-03-04T06:29:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=134"},"modified":"2024-02-02T17:23:10","modified_gmt":"2024-02-02T22:23:10","slug":"arraylist-and-iterator-example","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/arraylist-and-iterator-example\/","title":{"rendered":"ArrayList and Iterator Example"},"content":{"rendered":"\n<p>This example shows how to use an iterator with ArrayList. Iterator can be used with other collections.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\n\npublic class ArrayListIterator {\n    public static void main(String&#91;] args) {\n        ArrayList al = new ArrayList();\n        al.add(new Integer(11));\n        al.add(new Integer(13));\n        al.add(new Integer(17));\n\n        Iterator itr = al.iterator();\n        while (itr.hasNext()) {\n            System.out.println(itr.next());\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>11\n13\n17<\/code><\/pre>\n\n\n\n<p>Note that if you are using Java 5 or above, you will get the following warning.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Type ArrayList is a raw type. References to generic type ArrayList&lt;E> should be parameterized<\/code><\/pre>\n\n\n\n<p>This is because Java 5 introduced the concept of generics where you are encouraged to define types for interfaces. To remove these warnings, add type parameters as in the following example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\n\npublic class ArrayListIterator {\n    public static void main(String&#91;] args) {\n        ArrayList&lt;Integer> al = new ArrayList&lt;Integer>();\n        al.add(new Integer(11));\n        al.add(new Integer(13));\n        al.add(new Integer(17));\n\n        Iterator&lt;strong>&amp;lt;Integer&amp;gt;&lt;\/strong> itr = al.iterator();\n        while (itr.hasNext()) {\n            System.out.println(itr.next());\n        }\n    }\n}<\/code><\/pre>\n\n\n\n<p>output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>11\n13\n17<\/code><\/pre>\n\n\n\n<p>In this example, we created an ArrayList that stores Integer objects. Three Integers are added. An Iterator is instantiated for the ArrayList. We loop through the entire ArrayList and print each element. itr.next() returns a String which is printed by System.out.print.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This example shows how to use an iterator with ArrayList. Iterator can be used with other collections. output Note that if you are using Java 5 or above, you will get the following warning. This is because Java 5 introduced the concept of generics where you are encouraged to define types for interfaces. To remove [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[38],"class_list":["post-134","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=134"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/134\/revisions"}],"predecessor-version":[{"id":135,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/134\/revisions\/135"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}