<div dir="ltr" style="text-align: justify;">
<p>The <i><b>ArrayList </b></i>class extends <i><b>AbstractList </b></i>and implements the List interface. <b><i>ArrayList </i></b>supports dynamic arrays that can grow as needed.</p>
<p>Standard Java arrays are of a fixed length. After arrays are created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.</p>
<p>Array lists are created with an initial size. When this size is exceeded, the collection is automatically enlarged. When objects are removed, the array may be shrunk.<br />
<b><br />
</b> <b><i>ArrayList </i>class</b></p>
<ul>
<li><b><i>ArrayList </i></b>class extends <i><b>AbstractList </b></i>class and implements the <i><b>List </b></i>interface.</li>
<li><i><b>ArrayList </b></i>supports dynamic array that can grow as needed. <i><b>ArrayList </b></i>has three constructors.</li>
</ul>
<div id="ads-id" align="center"></div>
<pre class="highlight">//constructor builds an empty array list 
ArrayList() 
//The following constructor builds an array list that is initialized with the elements of the collection c. 
ArrayList( Collection C ) 
/*The following constructor builds an array list that has the specified initial capacity. The capacity is the size of the underlying array that is used to store the elements. 
 
The capacity grows automatically as elements are added to an array list.*/ 
ArrayList( int capacity ) 
</pre>
<ul>
<li><b>ArrayLists </b>are created with an initial size, when this size is exceeded, it gets enlarged automatically.</li>
<li>It can coontain Duplicate elements and maintains the insertion order.</li>
<li><i><b>ArrayLists </b></i>are not synchronized.</li>
</ul>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/arraylist.jpg" border="0" /></div>
<p><b>Methods in the <i>ArrayList </i>Class:</b></p>
<div style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: white; color: black; font-family: Helvetica, Arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; margin: 0.8em 0px 1em; orphans: auto; padding: 0px; text-align: justify; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px;">Apart from the methods inherited from its parent classes, <i><b>ArrayList </b></i>defines following methods:</div>
<table class="src" style="-webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; background-color: #f7f7f7; border-collapse: collapse; border: 1px solid #d6d6d6; color: black; font-family: Helvetica, Arial, sans-serif; font-size: 12px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; margin: 8px 0px; orphans: auto; padding: 0px; text-align: start; text-indent: 0px; text-transform: none; vertical-align: top; white-space: normal; widows: auto; width: 560px; word-spacing: 0px;">
<tbody style="margin: 0px; padding: 0px;">
<tr style="margin: 0px; padding: 0px;">
<th style="background-color: #eeeeee; border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px; text-align: left;">SN</th>
<th style="background-color: #eeeeee; border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px; text-align: left;">Methods with Description</th>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">1</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">void add(int index, Object element)</b><br />
Inserts the specified element at the specified position index in this list. Throws IndexOutOfBoundsException if the specified index is is out of range (index <; 0 || index >; size()).</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">2</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">boolean add(Object o) </b><br />
Appends the specified element to the end of this list.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">3</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">boolean addAll(Collection c)</b><br />
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection&#8217;s iterator. Throws NullPointerException if the specified collection is null.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">4</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">boolean addAll(int index, Collection c) </b><br />
Inserts all of the elements in the specified collection into this list, starting at the specified position. Throws NullPointerException if the specified collection is null.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">5</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">void clear() </b><br />
Removes all of the elements from this list.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">6</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">Object clone() </b><br />
Returns a shallow copy of this ArrayList.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">7</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">boolean contains(Object o) </b><br />
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null ? e==null : o.equals(e)).</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">8</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">void ensureCapacity(int minCapacity) </b><br />
Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">9</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">Object get(int index) </b><br />
Returns the element at the specified position in this list. Throws IndexOutOfBoundsException if the specified index is is out of range (index <; 0 || index >;= size()).</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">10</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">int indexOf(Object o) </b><br />
Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">11</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">int lastIndexOf(Object o)</b><br />
Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">12</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">Object remove(int index) </b><br />
Removes the element at the specified position in this list. Throws IndexOutOfBoundsException if index out of range (index <; 0 || index >;= size()).</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">13</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">protected void removeRange(int fromIndex, int toIndex) </b><br />
Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">14</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">Object set(int index, Object element) </b><br />
Replaces the element at the specified position in this list with the specified element. Throws IndexOutOfBoundsException if the specified index is is out of range (index <; 0 || index >;= size()).</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">15</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">int size() </b><br />
Returns the number of elements in this list.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">16</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">Object[] toArray() </b><br />
Returns an array containing all of the elements in this list in the correct order. Throws NullPointerException if the specified array is null.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">17</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">Object[] toArray(Object[] a) </b><br />
Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array.</td>
</tr>
<tr style="margin: 0px; padding: 0px;">
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;">18</td>
<td style="border-collapse: collapse; border: 1px solid #d6d6d6; margin: 0px; padding: 5px;"><b style="margin: 0px; padding: 0px;">void trimToSize() </b><br />
Trims the capacity of this ArrayList instance to be the list&#8217;s current size.</td>
</tr>
</tbody>
</table>
<p><b>Example of <i>ArrayList</i>:</b></p>
<pre class="highlight">import java.util.*; 
 
public class ArrayListDemo { 
 public static void main(String args[]) { 
 // create an array list 
 ArrayList list = new ArrayList(); 
 System.out.println("Initial size of list: " + list.size()); 
 
 // add elements to the array list 
 list.add("Dinesh"); 
 list.add("Sweety"); 
 list.add("Adesh"); 
 list.add("Vinesh"); 
 list.add("Mom"); 
 list.add("DAD"); 
 
 System.out.println("Size of list after additions: " + list.size()); 
 
 // display the array list 
 System.out.println("Contents of list: " + list); 
 
 System.out.println("Iterating list"); 
 Iterator itr = list.iterator(); 
 while(itr.hasNext()){ 
 System.out.println(itr.next()); 
 } 
 } 
} 
</pre>
<p><b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/arraylist.png" border="0" /></div>
<p><b>Two ways to iterate the elements of collection:</b></p>
<ol>
<li>By Iterator interface.</li>
<li>By for-each loop.</li>
</ol>
<p><b>Iterating the elements of Collection by for-each loop:</b></p>
<pre class="highlight">import java.util.*; 
 
public class ArrayListDemo { 
 public static void main(String args[]) { 
 // create an array list 
 ArrayList list = new ArrayList(); 
 
 // add elements to the array list 
 list.add("Dinesh"); 
 list.add("Sweety"); 
 list.add("Adesh"); 
 list.add("Vinesh"); 
 list.add("Mom"); 
 list.add("DAD"); 
 System.out.println("Iterating list using for each loop"); 
 for(Object obj : list){ 
 System.out.println(obj); 
 } 
 } 
} 
</pre>
<p><b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/arraylist2.png" border="0" /></div>
<p><b>Storing User-Defined classes:</b><br />
In the above example we are storing only string object in <b><i>ArrayList </i></b>collection. But You can store any type of object, including object of class that you create in Collection classes.<br />
<b>Example of storing User-Defined object</b></p>
<p><b>Employee class</b></p>
<pre class="highlight">class Employee 
{ 
 String firstName; 
 String lastName; 
 String phone; 
 
 public Employee(String firstName,String lastName,String phone) 
 { 
 this.firstName = firstName ; 
 this.lastName = lastName; 
 this.phone = phone; 
 } 
 
 public String toString() 
 { 
 return firstName +" "+lastName +" ("+phone +") "; 
 } 
} 
</pre>
<p><b>Storing Employee class</b></p>
<pre class="highlight">public class EmployeeListDemo 
{ 
 
 public static void main(String[] args) 
 { 
 Employee e1 = new Employee("Dinesh", "Rajput","999100091"); 
 Employee e2 = new Employee("Sandeep", "Sharma","998392819"); 
 Employee e3 = new Employee("Vinesh", "Kohli","998131319"); 
 
 ArrayList al = new ArrayList(); 
 al.add(e1); 
 al.add(e2); 
 al.add(e3); 
 System.out.println(al); 
 } 
 
} 
</pre>
<p><b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/arraylist3.png" border="0" /></div>
<p><b>Example of <i>addAll(Collection c)</i> method:</b></p>
<pre class="highlight">import java.util.*; 
class SimpleListDemo{ 
 public static void main(String args[]){ 
 
 ArrayList al = new ArrayList(); 
 al.add("Dinesh"); 
 al.add("Anamika"); 
 al.add("Sweety"); 
 
 ArrayList al2 = new ArrayList(); 
 al2.add("Adesh"); 
 al2.add("Vinesh"); 
 
 al.addAll(al2); 
 
 Iterator itr=al.iterator(); 
 while(itr.hasNext()){ 
 System.out.println(itr.next()); 
 } 
 } 
} 
</pre>
<p><b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/arraylist4.png" border="0" /></div>
<p><b>Example of <i>removeAll()</i> method:</b></p>
<pre class="highlight">import java.util.*; 
class SimpleListDemo{ 
 public static void main(String args[]){ 
 
 ArrayList al = new ArrayList(); 
 al.add("Dinesh"); 
 al.add("Anamika"); 
 al.add("Sweety"); 
 
 ArrayList al2 = new ArrayList(); 
 al2.add("Anamika"); 
 al2.add("Vinesh"); 
 
 al.removeAll(al2); 
 
 Iterator itr=al.iterator(); 
 while(itr.hasNext()){ 
 System.out.println(itr.next()); 
 } 
 } 
} 
</pre>
<p><b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/arraylist5.png" border="0" /></div>
<p><b>Example of <i>retainAll()</i> method:</b></p>
<pre class="highlight">import java.util.*; 
class SimpleListDemo{ 
 public static void main(String args[]){ 
 
 ArrayList al = new ArrayList(); 
 al.add("Dinesh"); 
 al.add("Anamika"); 
 al.add("Sweety"); 
 
 ArrayList al2 = new ArrayList(); 
 al2.add("Anamika"); 
 al2.add("Vinesh"); 
 System.out.println("iterating the elements after retaining the elements of al2..."); 
 al.retainAll(al2); 
 
 Iterator itr=al.iterator(); 
 while(itr.hasNext()){ 
 System.out.println(itr.next()); 
 } 
 } 
} 
</pre>
<p><b>output:</b></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/05/arraylist6.png" border="0" /></div>
<p> ;</p>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;">
<b>Java Collections Tutorial</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/linkedlist-class-in-collection/">LinkedList class</a></b></li>
<li><b><a href="https://dineshonjava.com/listiterator-interface-in-collection/">ListIterator interface</a></b></li>
<li><b><a href="https://dineshonjava.com/hashset-class-in-collection/">HashSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedhashset-class-in-collection/">LinkedHashSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/treeset-classin-collection/">TreeSet class</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-hashset-and-treeset/">Difference between TreeSet &; HashSet</a></b></li>
<li><b><a href="https://dineshonjava.com/map-interface-in-collection/">Map interface</a></b></li>
<li><b><a href="https://dineshonjava.com/hashmap-class-in-collection-framework/">HashMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/how-does-java-hashmap-work-internally/">How does work HashMap?</a> </b></li>
<li><b><a href="https://dineshonjava.com/difference-between-treemap-vs-hashmap/">Difference between TreeMap vs HashMap</a></b></li>
<li><b><a href="https://dineshonjava.com/linkedhashmap-class-in-collection/">LinkedHashMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/treemap-class-in-collection-framework/">TreeMap class</a></b></li>
<li><b><a href="https://dineshonjava.com/hashtable-class-in-collection-framework/">Hashtable class</a></b></li>
<li><b><a href="https://dineshonjava.com/difference-between-hashmap-and/">Difference between HashMap and HashTable in Java</a></b></li>
<li><b><a href="https://dineshonjava.com/sorting-in-collection-framework/">Sorting</a></b></li>
<li><b><a href="https://dineshonjava.com/java-comparable-and-comparator/">Comparable interface</a></b></li>
</ol>
</div>
<div style="background-color: pink; border-width: thin; text-align: center;"><b><;<;<a href="https://dineshonjava.com/java-collection-framework">Previous</a> <;<; || <a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/">Index </a>|| >;>;<a href="https://dineshonjava.com/linkedlist-class-in-collection/">Next</a> >;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/java-collection-framework/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/linkedlist-class-in-collection/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '468'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…