{"id":450,"date":"2016-02-27T13:57:39","date_gmt":"2016-02-27T13:57:39","guid":{"rendered":"http:\/\/abhiandroid.com\/java\/?page_id=450"},"modified":"2016-02-27T13:57:39","modified_gmt":"2016-02-27T13:57:39","slug":"arraylist","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/java\/arraylist","title":{"rendered":"ArrayList Tutorial With Examples In JAVA"},"content":{"rendered":"<p>ArrayList is a dynamic data structure in which you can add or remove any number of elements and those elements are stored in ordered sequence. It may also contain duplicate values.<\/p>\n<p>ArrayList are the implementations of <a href=\"\/java\/list\">List<\/a> interface. The package you required to import the ArrayList is <strong>import java.util.ArrayList;<\/strong><\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note: <\/strong><\/span>In ArrayList, the items to be added are only of <a href=\"\/java\/class-objects\">object<\/a> type. No <a href=\"\/java\/primitive-data-types-in-java\">primitive data types<\/a> (i.e. int, char etc) are allowed to insert in ArrayList.<\/p>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Syntax of ArrayList:<\/strong><\/span><\/h4>\n<p>Its very easy to use ArrayList, below is the <a href=\"\/java\/syntax.html\">syntax<\/a> to declare it:<\/p>\n<pre class=\"\">ArrayList arrayListDemo = new ArrayListDemo();<\/pre>\n<p><strong>String ArrayList Declaration:<\/strong><\/p>\n<pre>ArrayList&lt;String&gt; arrayListDemo = new ArrayListDemo&lt;String&gt;();<\/pre>\n<p>By using this syntax, an ArrayList is created which you can use to store characters. Here arrayListDemo is the name of this particular ArrayList.<\/p>\n<hr \/>\n<h4><strong>ArrayList Methods:<\/strong><\/h4>\n<p>ArrayList is a subclass of <strong>AbstractList<\/strong> class and it implements <strong><a href=\"\/java\/list\">List<\/a> Interface.<\/strong> It has various methods that are defined and inherited from its parent class. Below is the list of those methods with example:<\/p>\n<p><span style=\"color: #008000;\"><strong>1. boolean add(Object o):<\/strong><\/span><\/p>\n<ul>\n<li>It adds an element of Specific Object type at the end of Arraylist as no index is mentioned in the method.<\/li>\n<li>It returns True if element is successfully added, and returns false if it is not.<\/li>\n<\/ul>\n<p>The Example program of this boolean add(Object o) method is as:<\/p>\n<pre>import java.util.ArrayList;\r\n\r\npublic class ArrayListMethods {\r\n\r\npublic static void main(String[] args) {\r\n\u00a0\u00a0 \u00a0\r\n\u00a0\u00a0 \u00a0\/\/Integer ArrayList\r\nArrayList&lt;Integer&gt; aList = new ArrayList&lt;Integer&gt;();\r\n\r\naList.add(5);\r\naList.add(11);\r\naList.add(17);\r\n\r\nSystem.out.println(\"Integer Number Added in ArrayList= \" + aList);\r\n\r\n\/\/String ArrayList\r\nArrayList&lt;String&gt; sList = new ArrayList&lt;String&gt;();\r\n\r\nsList.add(\"Learning\");\r\nsList.add(\"JAVA\");\r\n\r\nSystem.out.println(\"String Added in ArrayList= \"+ sList);\r\n\r\n\r\n}\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>Integer Number Added in ArrayList= [5, 11, 17]\r\nString Added in ArrayList= [Learning, JAVA]\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>2. void clear():<\/strong><\/span><\/p>\n<p>This method remove all the elements of the arraylist.<\/p>\n<p>Below the example program clear() method shows the working of this method:<\/p>\n<pre>import java.util.ArrayList;\r\n\r\npublic class ArrayListMethods {\r\n\r\n\u00a0\u00a0 \u00a0public static void main(String args[]) {\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0ArrayList&lt;Integer&gt; aList = new ArrayList&lt;Integer&gt;();\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ use add() method to add elements in the list\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0aList.add(1);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0aList.add(2);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0aList.add(3);\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ let us print all the elements available in aList\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Printing aList items before using clear method= \"+aList);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Printing size of aList1= \" + aList.size());\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/using clear method\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0aList.clear();\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"Printing aList element after using clear method= \"+aList);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"size of aList1 after clear() method= \" + aList.size());\r\n\r\n\u00a0\u00a0 \u00a0}\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre class=\"\">Printing aList items before using clear method= [1, 2, 3]\r\nPrinting size of aList1= 3\r\nPrinting aList element after using clear method= []\r\nsize of aList1 after clear() method= 0<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. Object clone():<\/strong><\/span><\/p>\n<p>This method returns the exact same copy of the arraylist object.<\/p>\n<p>Below example helps us to understand this method easily:<\/p>\n<pre>import java.util.ArrayList;\r\n\r\npublic class ArrayListMethods {\r\n\u00a0\u00a0 \u00a0public static void main(String args[]) {\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0ArrayList&lt;String&gt; aList1 = new ArrayList&lt;String&gt;();\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ use add() method to add elements in the list\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0aList1.add(\"A\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0aList1.add(\"B\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0aList1.add(\"C\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0aList1.add(\"D\");\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/Using clone() method to copy aList1 into a new aListCopy Arraylist\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0ArrayList&lt;String&gt; aListCopy = (ArrayList&lt;String&gt;) aList1.clone();\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"aListCopy elements copied from aList1= \"+aListCopy);\r\n\r\n\u00a0\u00a0 \u00a0}\r\n}<\/pre>\n<p><strong>Output:\u00a0<\/strong><\/p>\n<pre>aListCopy elements copied from aList1= [A, B, C, D]<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. boolean contains(Object element):<\/strong><\/span><\/p>\n<p>This method returns true if the calling ArrayList object contains the specific element as given in the argument list, otherwise it returns false.<\/p>\n<p>Below is the example program of boolean contains(Object element) method is as:<\/p>\n<pre class=\"\">import java.util.ArrayList;\r\n\r\npublic class ArrayListMethods {\r\n\r\n\u00a0\u00a0 \u00a0public static void main(String args[]) {\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0ArrayList&lt;Integer&gt; aList = new ArrayList&lt;Integer&gt;();\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/ use add() method to add elements in the list\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0aList.add(7);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0aList.add(2);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0aList.add(9);\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/Checking contains method\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0boolean flag1 =\u00a0 aList.contains(2);\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if (flag1 == true) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"aList contains element 2\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}else{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"aList doesn't contains element 2\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0boolean flag2 = aList.contains(5);\r\n\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0if (flag2 == true) {\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"aList contains element 5\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0} else{\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0System.out.println(\"aList doesn't contains element 5\");\r\n\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0}\r\n\r\n\r\n\u00a0\u00a0 \u00a0}\r\n\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<pre>aList contains element 2\r\naList doesn't contains element 5<\/pre>\n<p>Read: <a href=\"\/java\/arraylist-methods-example.html\">List Of All ArrayList Methods<\/a><\/p>\n<hr \/>\n<h4><strong>ArrayList Example In Java:<\/strong><\/h4>\n<p><span style=\"color: #008000;\"><strong>Example 1: ArrayList of Pre Definded Data types:<\/strong><\/span><\/p>\n<p>Let us discuss ArrayList with the help of program, In this program we have created two objects that store predefined data types that are of Integer and String type, following program is divided into 3 steps that are discussed below:<\/p>\n<pre>import java.util.*; \r\n\r\n\r\npublic class ArrayListDemo{ \r\n \r\n public static void main(String args[]){ \r\n \r\n \/\/Step 1: Creating Objects of ArrayList\r\n ArrayList&lt;String&gt; arraylist1= new ArrayList&lt;String&gt;();\r\n ArrayList&lt;Integer&gt; arraylist2= new ArrayList&lt;Integer&gt;();\r\n\r\n \/\/Step 2: Adding Values \r\n arraylist1.add(\"This\");\r\n arraylist1.add(\"is\");\r\n arraylist1.add(\"String\");\r\n arraylist1.add(\"arraylist\");\r\n \r\n arraylist2.add(1);\r\n arraylist2.add(2);\r\n arraylist2.add(3);\r\n arraylist2.add(4);\r\n \r\n \/\/Step 3: Displaying all the values\r\n System.out.println(\"String ArrayList : \" +arraylist1);\r\n System.out.println(\"Integer ArrayList : \" +arraylist2);\r\n } \r\n}<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre>String ArrayList : [This, is, String, arraylist]\r\nInteger ArrayList : [1, 2, 3, 4]<\/pre>\n<p><span style=\"color: #008000;\"><strong>Explanation of Example:<\/strong><\/span><\/p>\n<ul>\n<li>In Step 1, we have created two objects of ArrayList Class, first object stores value of String type, Second of Integer type.<\/li>\n<li>In Step 2, we have used add method to store values in the data structures that we have created in step 1.<\/li>\n<li>In Step 3, we have displayed values of these objects.<\/li>\n<\/ul>\n<p><strong><span style=\"color: #008000;\">Example 2: ArrayList Of User-Defined Objects:<\/span><\/strong><\/p>\n<p>We have discussed ArrayList example with Pre Defined Data types like Integer and String types, now let us discuss Arraylist of User Defined Class.<\/p>\n<p>Now, Let us take a Class Student that has two properties its Name and Age. What we will do is, we will create different student objects and store them in the ArrayList as shown in the following program.<\/p>\n<pre class=\"\">import java.util.*;\r\n\r\n\/\/Step 1: Created a Class that have values name and age\r\n\r\nclass Student{\r\nprivate int age;\r\nprivate String name;\r\n\r\n\/\/Step 2: Creating Constructor of Student class\r\npublic Student( String name, int age) {\r\nthis.age = age;\r\nthis.name = name;\r\n}\r\n\r\n\/\/Step 3: Getting and Setting the values\r\npublic int getAge() {\r\nreturn age;\r\n}\r\n\r\npublic void setAge(int age) {\r\nthis.age = age;\r\n}\r\npublic String getName() {\r\nreturn name;\r\n}\r\npublic void setName(String name) {\r\nthis.name = name;\r\n}\r\n\r\n\/\/Step 4: Gives meaning to the object\r\n\r\n@Override\r\n\r\npublic String toString() {\r\n\r\nreturn \"Student [name=\" + name + \", age=\" + age + \"]\";\r\n\r\n}\r\n}<\/pre>\n<p>Creating the main class ArrayListDemo:<\/p>\n<pre>import java.util.*;\r\n\/\/Step 5: Creating the main class\r\n\r\npublic class ArrayListDemo{\r\n\r\npublic static void main(String args[]){\r\n\r\n\/\/Step 6: Creating objects of Student class\r\n\r\nStudent student1= new Student(\"Vikas\" , 24);\r\nStudent student2= new Student(\"Arun\" , 23);\r\nStudent student3= new Student(\"Rahul\" , 25);\r\n\r\n\r\n\/\/Step 7: Creating an object of ArrayList and adding Student objects\r\n\r\nArrayList&lt;Student&gt; arrayStudent = new ArrayList&lt;Student&gt;();\r\n\r\narrayStudent.add(student1);\r\narrayStudent.add(student2);\r\narrayStudent.add(student3);\r\n\r\n\/\/Step 8: Displaying the values\r\n\r\nfor(Student student: arrayStudent){\r\n\r\nSystem.out.println(student);\r\n}\r\n}\r\n}\r\n\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Output:<\/strong><\/span><\/p>\n<pre class=\"\">Student [name=Vikas, age=24]\r\nStudent [name=Arun, age=23]\r\nStudent [name=Rahul, age=25]\r\n\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Description:<\/strong><\/span><\/p>\n<ul>\n<li>In Step 1, we have created a class Student , that defined two parameters name and age.<\/li>\n<li>In Step 2, we have created constructor of Student class, that will assign values of name and age when we define the object in main class.<\/li>\n<li>In Step 3, we have used getter and setter methods to set and get the values of name and age.<\/li>\n<li>In Step 4, <strong>toString()<\/strong> method has been overridden, to give meaning to the object, \u00a0so that when we print the object using\u00a0<strong>System.out.println,<\/strong>\u00a0we can see which object it really is.<\/li>\n<li>In Step 5, we have created the main class.<\/li>\n<li>In Step 6, we have created three objects of Student Class, and given values to them.<\/li>\n<li>In Step 7, an object of ArrayList that is of type Student is created and above created objects has been added into them.<\/li>\n<li>In Step 8, All the values has been displayed.<\/li>\n<\/ul>\n<hr \/>\n<h4><span style=\"color: #008000;\"><strong>Importance of ArrayList:<\/strong><\/span><\/h4>\n<ul>\n<li>You can define ArrayList as re-sizable <a href=\"\/java\/arrays\">array<\/a>. It means size of the ArrayList is not fixed, it can grow and shrink dynamically.<\/li>\n<li>In ArrayList elements can be inserted at or deleted from a particular position.<\/li>\n<li>If generics are not used, ArrayList can hold any type of objects.<\/li>\n<li>You can traverse an\u00a0ArrayList in both the directions \u2013 forward and backward using ListIterator<\/li>\n<li>ArrayList can hold multiple null and duplicate elements.<\/li>\n<li>ArrayList provide sufficient methods to work with like Add().<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>Quick Revision Points Of ArrayList:<\/strong><\/h4>\n<ul>\n<li>ArrayList is dynamic data structure in which you can add or remove any number of elements.<\/li>\n<li>The package you required to import the ArrayList is <strong>import java.util.ArrayList;<\/strong><\/li>\n<li>AddAll method is having the special feature to add up the all elements of one list to the other.<\/li>\n<\/ul>\n<hr \/>\n<h4><strong>ArrayList Vs Array: Which To Use When:<\/strong><\/h4>\n<p>Another important thing about ArrayList is when to use it or when to use simple <a href=\"\/java\/arrays\">Array<\/a>? Now as you know ArrayList is dynamic in nature which means you can add or delete the elements with no fixed lower limit and upper limit. So in those situation where you are not sure about the number of elements you should use ArrayList. And where you are clear with the exact size of elements you can use Array. This is because array is fixed in size after its declaration\u00a0and\u00a0ArrayList is resizable in nature.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ArrayList is a dynamic data structure in which you can add or remove any number of elements and those elements are stored in ordered sequence. It may also contain duplicate values. ArrayList are the implementations of List interface. The package you required to import the ArrayList is import java.util.ArrayList; Important Note: In ArrayList, the items &hellip; <a href=\"https:\/\/abhiandroid.com\/java\/arraylist\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">ArrayList Tutorial With Examples In JAVA<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"home.php","meta":{"footnotes":""},"class_list":["post-450","page","type-page","status-publish","hentry"],"psp_head":"<title>ArrayList Tutorial With Examples In JAVA \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"ArrayList is a dynamic data structure in which you can add or remove any number of elements and those elements are stored in ordered sequence and it may also contain duplicate values.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/java\/arraylist\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/comments?post=450"}],"version-history":[{"count":0,"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/pages\/450\/revisions"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/java\/wp-json\/wp\/v2\/media?parent=450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}