{"id":144,"date":"2020-03-12T06:54:00","date_gmt":"2020-03-12T06:54:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=144"},"modified":"2024-02-06T11:49:32","modified_gmt":"2024-02-06T16:49:32","slug":"accessing-arraylist-elements","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/accessing-arraylist-elements\/","title":{"rendered":"Accessing ArrayList Elements"},"content":{"rendered":"\n<p>To access a single ArrayList element, use ArrayList.get() method. To print all elements, you can use a simple for loop with ArrayList.get() method or use an Iterator object to iterate through the ArrayList.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.*;\n\npublic class ArrayListPrint {\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    \/\/ prints third element\n    System.out.println(al.get(2));\n\n    \/\/ prints all elements\n    for (int i = 0; i &lt; al.size(); i++) {\n       System.out.println(al.get(i));\n    }\n\n    \/\/ uses Iterator to print all elements\n    Iterator&lt;Integer> itr = al.iterator();\n    while (itr.hasNext()) {\n       System.out.println(itr.next());\n    }\n  }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>To access a single ArrayList element, use ArrayList.get() method. To print all elements, you can use a simple for loop with ArrayList.get() method or use an Iterator object to iterate through the ArrayList.<\/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,76],"class_list":["post-144","post","type-post","status-publish","format-standard","hentry","category-java","tag-java","tag-programming"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/144","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=144"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/144\/revisions"}],"predecessor-version":[{"id":145,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/144\/revisions\/145"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=144"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=144"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=144"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}