{"id":1255,"date":"2022-02-26T06:57:45","date_gmt":"2022-02-26T06:57:45","guid":{"rendered":"https:\/\/mdr.foobrdigital.com\/?p=1255"},"modified":"2022-02-26T06:57:45","modified_gmt":"2022-02-26T06:57:45","slug":"arraylist-class","status":"publish","type":"post","link":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/2022\/02\/26\/arraylist-class\/","title":{"rendered":"\u00a0ArrayList Class"},"content":{"rendered":"\n<p>In this tutorial, we will learn about the ArrayList class in Java. We will learn about different operations and methods of the arraylist with the help of examples.<\/p>\n\n\n\n<p id=\"introduction\">The&nbsp;<code>ArrayList<\/code>&nbsp;class of the Java collections framework provides the functionality of&nbsp;<strong>resizable-arrays<\/strong>.<\/p>\n\n\n\n<p>It implements the&nbsp;<code>List<\/code>&nbsp;interface.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/cdn.programiz.com\/cdn\/farfuture\/fyE7ehxccOWAmdgGrQzyHmSCnPu-c6LSIbJ85hXyC0A\/mtime:1602144358\/sites\/tutorial2program\/files\/java-arraylist-implements-list.png\" alt=\"The List interface extends the Collection interface and the ArrayList class implements List.\" title=\"Java ArrayList Implementation\"\/><figcaption>Java ArrayList Implementation<\/figcaption><\/figure>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Java ArrayList Vs Array<\/h2>\n\n\n\n<p>In Java, we need to declare the size of an array before we can use it. Once the size of an array is declared, it&#8217;s hard to change it.<\/p>\n\n\n\n<p>To handle this issue, we can use the&nbsp;<code>ArrayList<\/code>&nbsp;class. It allows us to create resizable arrays.<\/p>\n\n\n\n<p>Unlike arrays, arraylists can automatically adjust its capacity when we add or remove elements from it. Hence, arraylists are also known as&nbsp;<strong>dynamic arrays<\/strong>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"create\">Creating an ArrayList<\/h2>\n\n\n\n<p>Before using&nbsp;<code>ArrayList<\/code>, we need to import the&nbsp;<code>java.util.ArrayList<\/code>&nbsp;package first. Here is how we can create arraylists in Java:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ArrayList&lt;Type&gt; arrayList= new ArrayList&lt;&gt;();<\/code><\/pre>\n\n\n\n<p>Here,&nbsp;<var>Type<\/var>&nbsp;indicates the type of an arraylist. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ create Integer type arraylist\nArrayList&lt;Integer&gt; arrayList = new ArrayList&lt;&gt;();\n\n\/\/ create String type arraylist\nArrayList&lt;String&gt; arrayList = new ArrayList&lt;&gt;();<\/code><\/pre>\n\n\n\n<p>In the above program, we have used&nbsp;<code>Integer<\/code>&nbsp;not int. It is because we cannot use primitive types while creating an arraylist. Instead, we have to use the corresponding wrapper classes.<\/p>\n\n\n\n<p>Here,\u00a0<code>Integer<\/code>\u00a0is the corresponding wrapper class of\u00a0<code>int<\/code>.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"example\">Example: Create ArrayList in Java<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\n\nclass Main {\n  public static void main(String&#91;] args){\n\n    \/\/ create ArrayList\n    ArrayList&lt;String&gt; languages = new ArrayList&lt;&gt;();\n\n    \/\/ Add elements to ArrayList\n    languages.add(\"Java\");\n    languages.add(\"Python\");\n    languages.add(\"Swift\");\n    System.out.println(\"ArrayList: \" + languages);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>ArrayList: &#91;Java, Python, Swift]<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an&nbsp;<code>ArrayList<\/code>&nbsp;named&nbsp;<var>languages<\/var>.<\/p>\n\n\n\n<p>Here, we have used the&nbsp;<code>add()<\/code>&nbsp;method to add elements to the arraylist. We will learn more about the&nbsp;<code>add()<\/code>&nbsp;method later in this tutorial.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Operations on ArrayList<\/h2>\n\n\n\n<p>The&nbsp;<code>ArrayList<\/code>&nbsp;class provides various methods to perform different operations on arraylists. We will look at some commonly used arraylist operations in this tutorial:<\/p>\n\n\n\n<ul><li>Add elements<\/li><li>Access elements<\/li><li>Change elements<\/li><li>Remove elements<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"add-element\">1. Add Elements to an ArrayList<\/h3>\n\n\n\n<p>To add a single element to the arraylist, we use the&nbsp;<code>add()<\/code>&nbsp;method of the&nbsp;<code>ArrayList<\/code>&nbsp;class. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\n\nclass Main {\n  public static void main(String&#91;] args){\n    \/\/ create ArrayList\n    ArrayList&lt;String&gt; languages = new ArrayList&lt;&gt;();\n\n    \/\/ add() method without the index parameter\n    languages.add(\"Java\");\n    languages.add(\"C\");\n    languages.add(\"Python\");\n    System.out.println(\"ArrayList: \" + languages);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><samp>ArrayList: [Java, C, Python]<\/samp><\/pre>\n\n\n\n<p>In the above example, we have created an&nbsp;<code>ArrayList<\/code>&nbsp;named&nbsp;<var>languages<\/var>. Here, we have used the&nbsp;<code>add()<\/code>&nbsp;method to add elements to&nbsp;<var>languages<\/var>.<\/p>\n\n\n\n<p>To learn more, visit the&nbsp;<a href=\"https:\/\/www.programiz.com\/java-programming\/library\/arraylist\/add\">Java ArrayList add()<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Other way to add elements to arraylist<\/h2>\n\n\n\n<p>How to add an element at a specified position in an ArrayList?<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>We can also pass an&nbsp;<strong>index number<\/strong>&nbsp;as an additional parameter to the&nbsp;<code>add()<\/code>&nbsp;method to add an element at the specified position. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ add JavaScript at index 1\nlanguages.add(1, \"JavaScript\");\n\n\/\/ add C++ at index 3\nlanguages.add(3, \"C++\");<\/code><\/pre>\n\n\n\n<p>How to add all elements of a set to an arraylist?<\/p>\n\n\n\n<p>We can also add all elements of a collection (set, map) to an arraylist using the&nbsp;<code>addAll()<\/code>&nbsp;method. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\nimport java.util.HashSet;\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ create a set\n    HashSet&lt;String&gt; vowels = new HashSet&lt;&gt;();\n    vowels.add(\"a\");\n    vowels.add(\"e\");\n    vowels.add(\"i\");\n\n    \/\/ create an arraylist\n    ArrayList&lt;String&gt; alphabets = new ArrayList&lt;&gt;();\n\n    \/\/ add all elements of set to arraylist\n    alphabets.addAll(vowels);\n    System.out.println(\"ArrayList: \" + alphabets);\n  }\n}\n\n\/\/ Output: ArrayList: &#91;a, e, i]<\/code><\/pre>\n\n\n\n<p><a href=\"https:\/\/www.programiz.com\/java-programming\/library\/arraylist\/addall\"><\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"access-element\">2. Access ArrayList Elements<\/h3>\n\n\n\n<p>To access an element from the arraylist, we use the&nbsp;<code>get()<\/code>&nbsp;method of the&nbsp;<code>ArrayList<\/code>&nbsp;class. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\n\nclass Main {\n  public static void main(String&#91;] args) {\n    ArrayList&lt;String&gt; animals = new ArrayList&lt;&gt;();\n\n    \/\/ add elements in the arraylist\n    animals.add(\"Cat\");\n    animals.add(\"Dog\");\n    animals.add(\"Cow\");\n    System.out.println(\"ArrayList: \" + animals);\n\n    \/\/ get the element from the arraylist\n    String str = animals.get(1);\n    System.out.print(\"Element at index 1: \" + str);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>ArrayList: &#91;Cat, Dog, Cow]\nElement at index 1: Dog<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have used the&nbsp;<code>get()<\/code>&nbsp;method with parameter&nbsp;<var>1<\/var>. Here, the method returns the element at&nbsp;<strong>index 1<\/strong>.<\/p>\n\n\n\n<p>To learn more, visit the\u00a0Java ArrayList get().<\/p>\n\n\n\n<p>We can also access elements of the\u00a0<code>ArrayList<\/code>\u00a0using the\u00a0<code>iterator()<\/code>\u00a0method. To learn more, visit\u00a0Java ArrayList iterator().<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"change-element\">3. Change ArrayList Elements<\/h3>\n\n\n\n<p>To change elements of the arraylist, we use the&nbsp;<code>set()<\/code>&nbsp;method of the&nbsp;<code>ArrayList<\/code>&nbsp;class. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\n\nclass Main {\n  public static void main(String&#91;] args) {\n    ArrayList&lt;String&gt; languages = new ArrayList&lt;&gt;();\n\n    \/\/ add elements in the array list\n    languages.add(\"Java\");\n    languages.add(\"Kotlin\");\n    languages.add(\"C++\");\n    System.out.println(\"ArrayList: \" + languages);\n\n    \/\/ change the element of the array list\n    languages.set(2, \"JavaScript\");\n    System.out.println(\"Modified ArrayList: \" + languages);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>ArrayList: &#91;Java, Kotlin, C++]\nModified ArrayList: &#91;Java, Kotlin, JavaScript]<\/samp><\/code><\/pre>\n\n\n\n<p>In the above example, we have created an&nbsp;<code>ArrayList<\/code>&nbsp;named&nbsp;<var>languages<\/var>. Notice the line,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>language.set(2, \"JavaScript\");<\/code><\/pre>\n\n\n\n<p>Here, the&nbsp;<code>set()<\/code>&nbsp;method changes the element at&nbsp;<strong>index 2<\/strong>&nbsp;to&nbsp;<var>JavaScript<\/var>.<\/p>\n\n\n\n<p>To learn more, visit the\u00a0Java ArrayList set().<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"remove-elements\">4. Remove ArrayList Elements<\/h3>\n\n\n\n<p>To remove an element from the arraylist, we can use the&nbsp;<code>remove()<\/code>&nbsp;method of the&nbsp;<code>ArrayList<\/code>&nbsp;class. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\n\nclass Main {\n  public static void main(String&#91;] args) {\n    ArrayList&lt;String&gt; animals = new ArrayList&lt;&gt;();\n\n    \/\/ add elements in the array list\n    animals.add(\"Dog\");\n    animals.add(\"Cat\");\n    animals.add(\"Horse\");\n    System.out.println(\"ArrayList: \" + animals);\n\n    \/\/ remove element from index 2\n    String str = animals.remove(2);\n    System.out.println(\"Updated ArrayList: \" + animals);\n    System.out.println(\"Removed Element: \" + str);\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>ArrayList: &#91;Dog, Cat, Horse]\nUpdated ArrayList: &#91;Dog, Cat]\nRemoved Element: Horse<\/samp><\/code><\/pre>\n\n\n\n<p>Here, the&nbsp;<code>remove()<\/code>&nbsp;method takes the&nbsp;<strong>index number<\/strong>&nbsp;as the parameter. And, removes the element specified by the&nbsp;<strong>index number<\/strong>.<\/p>\n\n\n\n<p>To learn more, visit the\u00a0Java ArrayList remove().<\/p>\n\n\n\n<p>We can also remove all the elements from the arraylist at once. To learn more, visit<\/p>\n\n\n\n<ul><li>Java ArrayList removeAll()<\/li><li>Java ArrayList clear()<\/li><\/ul>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"methods\">Methods of ArrayList Class<\/h2>\n\n\n\n<p>In the previous section, we have learned about the&nbsp;<code>add()<\/code>,&nbsp;<code>get()<\/code>,&nbsp;<code>set()<\/code>, and&nbsp;<code>remove()<\/code>&nbsp;method of the&nbsp;<code>ArrayList<\/code>&nbsp;class.<\/p>\n\n\n\n<p>Besides those basic methods, here are some more&nbsp;<code>ArrayList<\/code>&nbsp;methods that are commonly used.<\/p>\n\n\n\n<figure class=\"wp-block-table is-style-regular\"><table><tbody><tr><th>Methods<\/th><th>Descriptions<\/th><\/tr><tr><td><a href=\"https:\/\/www.programiz.com\/java-programming\/library\/arraylist\/size\">size()<\/a><\/td><td>Returns the length of the arraylist.<\/td><\/tr><tr><td><a href=\"https:\/\/www.programiz.com\/java-programming\/library\/arraylist\/sort\">sort()<\/a><\/td><td>Sort the arraylist elements.<\/td><\/tr><tr><td><a href=\"https:\/\/www.programiz.com\/java-programming\/library\/arraylist\/clone\">clone()<\/a><\/td><td>Creates a new arraylist with the same element, size, and capacity.<\/td><\/tr><tr><td><a href=\"https:\/\/www.programiz.com\/java-programming\/library\/arraylist\/contains\">contains()<\/a><\/td><td>Searches the arraylist for the specified element and returns a boolean result.<\/td><\/tr><tr><td><a href=\"https:\/\/www.programiz.com\/java-programming\/library\/arraylist\/ensurecapacity\">ensureCapacity()<\/a><\/td><td>Specifies the total element the arraylist can contain.<\/td><\/tr><tr><td><a href=\"https:\/\/www.programiz.com\/java-programming\/library\/arraylist\/isempty\">isEmpty()<\/a><\/td><td>Checks if the arraylist is empty.<\/td><\/tr><tr><td><a href=\"https:\/\/www.programiz.com\/java-programming\/library\/arraylist\/indexof\">indexOf()<\/a><\/td><td>Searches a specified element in an arraylist and returns the index of the element.<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"loop\">Iterate through an ArrayList<\/h2>\n\n\n\n<p>We can use the&nbsp;<a href=\"https:\/\/www.programiz.com\/java-programming\/enhanced-for-loop\">Java for-each loop<\/a>&nbsp;to loop through each element of the arraylist. For example,<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.ArrayList;\n\nclass Main {\n  public static void main(String&#91;] args) {\n\n    \/\/ creating an array list\n    ArrayList&lt;String&gt; animals = new ArrayList&lt;&gt;();\n    animals.add(\"Cow\");\n    animals.add(\"Cat\");\n    animals.add(\"Dog\");\n    System.out.println(\"ArrayList: \" + animals);\n\n    \/\/ iterate using for-each loop\n    System.out.println(\"Accessing individual elements:  \");\n\n    for (String language : animals) {\n      System.out.print(language);\n      System.out.print(\", \");\n    }\n  }\n}<\/code><\/pre>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><samp>ArrayList: &#91;Cow, Cat, Dog]\nAccessing individual elements:  \nCow, Cat, Dog,<\/samp><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will learn about the ArrayList class in Java. We will learn about different operations and methods of the arraylist with the help of examples. The&nbsp;ArrayList&nbsp;class of the Java collections framework provides the functionality of&nbsp;resizable-arrays. It implements the&nbsp;List&nbsp;interface. Java ArrayList Vs Array In Java, we need to declare the size of an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[584],"tags":[],"_links":{"self":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1255"}],"collection":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/comments?post=1255"}],"version-history":[{"count":0,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/posts\/1255\/revisions"}],"wp:attachment":[{"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/media?parent=1255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/categories?post=1255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mudassirbackup.infinitycodestudio.com\/index.php\/wp-json\/wp\/v2\/tags?post=1255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}