# Java Programming/ArrayList

* Navigate Aggregate topic: ( v • d • e )
* Collection
* ArrayList
* Map
* Comparing Objects
The ArrayList class extends AbstractList and implements the List interface. ArrayList supports dynamic arrays that can grow as needed.
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.
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.

## Initializing

The ArrayList class supports three constructors. The first constructor builds an empty array list.:
The following constructor builds an array list that is initialized with the elements of the 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.

## Methods

ArrayList defines following methods:

### Adding Element in ArrayList

* Inserts the specified element at the specified position in the list. The existing element at the position and all the elements below it are shifted (this is the difference from the set method with the same parameters). Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 || index >= size).
* Appends the specified element to the end of this list.
* 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's iterator. Throws NullPointerException if the specified collection is null.
* 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.

### Size of ArrayList

* Returns the number of elements in this list.
Adding Element and Size of ArrayList
Output for Adding Element and Size of ArrayList
* Initial ArrayList :
* Size of al :2
* Contents of al :[A, B]
* Contents of al :[A, C, B]
* Size of al :3

### Get and Set ArrayList Element

* Returns the element at the specified position in this list. Throws IndexOutOfBoundsException if the specified index is out of range (index < 0 or index >= size).
* 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 or index >= size).

### Find Index of ArrayList Element

* Returns the index in this list of the first occurrence of the specified element, or -1 if the List does not contain this element.
* Returns the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element.

### Find Element Contain in ArrayList

* 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)).
Different Method in ArrayList
Output for Different Method in ArrayList
* Contents of al : [A, B, C, A, D, A, E]
* Index of D : 4
* Index of A : 0
* Index of A : 5
* Element at Second Index : C
* Element at Sixth Index : E
* Contents of al : [A, B, C, B, D, A, E]
* ArrayList contain D : true
* ArrayList contain F : false
Question: Consider the following code:
* public class ArrayListDemo {
* public static void main(String args) {
* ArrayList al = new ArrayList;
* al.add("A");
* al.add("B");
* al.add("C");
* al.add("E");
* al.add("F");
* al.remove(2);
* al.remove("F");
* al.set(1, "G");
* al.add("H");
* al.set(3, "I");
* System.out.println("Size of al : " + al.size);
* System.out.println("Contents of al : " + al);
In the example above, what is output?
* Size of al : 4
* Contents of al : [A, G, E, I]
Some more ArrayList methods:
| Method | Description |
| Object clone | Returns a shallow copy of this ArrayList. |
| Object toArray | Returns an array containing all of the elements in this list in the correct order. Throws NullPointerException if the specified array is null. |
| void trimToSize | Trims the capacity of this ArrayList instance to be the list's current size. |
| void ensureCapacity(int minCapacity) | 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. |
| protected void removeRange(int fromIndex, int toIndex) | Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive. |