{"id":900,"date":"2010-04-22T12:03:13","date_gmt":"2010-04-22T11:03:13","guid":{"rendered":"http:\/\/www.codingfriends.com\/?p=900"},"modified":"2010-04-22T19:22:09","modified_gmt":"2010-04-22T18:22:09","slug":"arraylist-and-listiterator","status":"publish","type":"post","link":"https:\/\/www.codingfriends.com\/index.php\/2010\/04\/22\/arraylist-and-listiterator\/","title":{"rendered":"ArrayList and ListIterator"},"content":{"rendered":"<p>An <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/util\/ArrayList.html\">ArrayList<\/a> is basically a array of objects that are managed within a <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/util\/List.html\">List<\/a> interface.  This basically means that Java will do the management of the List and you just need to either add to that list\/change the values\/remove items.<\/p>\n<p>The way that the ArrayList is defined is using the <a href=\"http:\/\/www.codingfriends.com\/index.php\/2009\/07\/27\/generics-2\/\">Generics<\/a> syntax, since a ArrayList can be of any type.<\/p>\n<pre lang=\"java\">\r\nArrayList<String> names = new ArrayList<String>();\r\n<\/pre>\n<p>this will create a object called &#8220;names&#8221; that have a array of type String and to add names to the array you use the &#8220;add&#8221; method, the &#8220;add&#8221; method can either just have the ArrayList type (in this case a String) as a single parameter or a index into the ArrayList with the ArrayList type again.. e.g.<\/p>\n<pre lang=\"java\">\r\n\/\/ normal add\r\nnames.add(\"Genux\");\r\n\/\/ adding at a set point\r\nnames.add(\"1\",\"Genux at point 1\");\r\n<\/pre>\n<p>to remove at a set point, there is a method called &#8220;remove&#8221; that will either take a index point or a class (e.g. will compare against a class within the ArrayList to see if there is anything that equals, if so remove from the ArrayList), in the example below I am just going to use the index point to remove from the ArrayList<\/p>\n<pre lang=\"java\">\r\nnames.remove(0);\r\n<\/pre>\n<p>you can also alter any of the names within the ArrayList with using the method &#8220;set&#8221;, here I am altering the index point 0 within the array to have the String value of &#8220;Genux new start&#8221;<\/p>\n<pre lang=\"java\">\r\nnames.set(0,\"Genux new start\");\r\n<\/pre>\n<p>The ListIterator is when you can iterate through a List (interface implemented object e.g ArrayList), to iterate through a list you basically go from one to the next to the next etc.  the ArrayList object called names has a method that will return the list iterator object for the ListIterator to go through, the hasNext method within the ListIterator returns true if there is another value\/object after the present one, the &#8220;next&#8221; method within the ListIterator returns a value\/object of the type of ListIterator and then goes to the next value within the List, here is some code that uses the above that uses the hasNext and next methods.<\/p>\n<pre lang=\"java\">\r\nListIterator<String> arrayListIt = names.listIterator();\r\nwhile (arrayListIt.hasNext())\r\n{\r\n\tSystem.out.println(\"Names :\"+ arrayListIt.next());\r\n}\r\n<\/pre>\n<p>here is the source code in full, if you save as &#8220;ArrayListTutorial.java&#8221;<\/p>\n<pre lang=\"java\">\r\nimport java.util.ArrayList;\r\nimport java.util.ListIterator;\r\n\r\npublic class ArrayListTutorial {\r\n\r\n\tpublic static void main(String[] args) {\r\n\t\t\tArrayList<String> names = new ArrayList<String>();\r\n\t\t\t\/\/ add names in a standard way, placing on the end in this case the start position = 0 \r\n\t\t\tnames.add(\"Genux\");\r\n\t\t\t\/\/ adding a set point\r\n\t\t\tnames.add(1,\"Genux at point 1\");\r\n\t\t\t\r\n\t\t\tSystem.out.println(\"Starting point of the Array of names\");\r\n\t\t\t\r\n\t\t\t\/\/ a ListIterator is a template (e.g. can use any type String\/Integer etc) \r\n\t\t\t\/\/ that will loop through a List object (in this case the ArrayList has implemented a List interface)\r\n\t\t\tListIterator<String> arrayListIt = names.listIterator();\r\n\t\t\twhile (arrayListIt.hasNext())\r\n\t\t\t{\r\n\t\t\t\tSystem.out.println(\"Names :\"+ arrayListIt.next());\r\n\t\t\t}\r\n\r\n\t\t\tSystem.out.println(\"Alerting the starting name\");\r\n\t\t\t\/\/ change the start of the array list e.g. point 0\r\n\t\t\tnames.set(0,\"Genux new start\");\r\n\t\t\t\r\n\t\t\t\/\/ loop through the array list with a iterator\r\n\t\t\tarrayListIt = names.listIterator();\r\n\t\t\twhile (arrayListIt.hasNext())\r\n\t\t\t{\r\n\t\t\t\tSystem.out.println(\"Names :\"+ arrayListIt.next());\r\n\t\t\t}\r\n\t\t\t\r\n\t\t\tSystem.out.println(\"Removing index 0 from the ArrayList of names\");\r\n\t\t\tnames.remove(0);\r\n\t\t\t\/\/ loop through the array list with a iterator\r\n\t\t\tarrayListIt = names.listIterator();\r\n\t\t\twhile (arrayListIt.hasNext())\r\n\t\t\t{\r\n\t\t\t\tSystem.out.println(\"Names :\"+ arrayListIt.next());\r\n\t\t\t}\r\n\t}\r\n}\r\n<\/pre>\n<p>and the output would be<\/p>\n<pre lang=\"bash\">\r\nStarting point of the Array of names\r\nNames :Genux\r\nNames :Genux at point 1\r\nAlerting the starting name\r\nNames :Genux new start\r\nNames :Genux at point 1\r\nRemoving index 0 from the ArrayList of names\r\nNames :Genux at point 1<\/pre>\n<p>to compile if you save the java code above as &#8220;ArrayListTutorial.java&#8221;<\/p>\n<pre lang=\"bash\">\r\njavac ArrayListTutorial.java\r\njava ArrayListTutorial\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>An ArrayList is basically a array of objects that are managed within a List interface. This basically means that Java will do the management of the List and you just need to either add to that list\/change the values\/remove items. The way that the ArrayList is defined is using the Generics syntax, since a ArrayList &hellip; <a href=\"https:\/\/www.codingfriends.com\/index.php\/2010\/04\/22\/arraylist-and-listiterator\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ArrayList and ListIterator<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[186,187],"class_list":["post-900","post","type-post","status-publish","format-standard","hentry","category-java","tag-arraylist","tag-listiterator"],"_links":{"self":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/900","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/comments?post=900"}],"version-history":[{"count":3,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/900\/revisions"}],"predecessor-version":[{"id":902,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/posts\/900\/revisions\/902"}],"wp:attachment":[{"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/media?parent=900"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/categories?post=900"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.codingfriends.com\/index.php\/wp-json\/wp\/v2\/tags?post=900"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}