Java Programming/ArrayList

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  method with the same parameters). Throws   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  if the specified collection is null.
 * Inserts all of the elements in the specified collection into this list, starting at the specified position. Throws  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

Get and Set ArrayList Element

 * Returns the element at the specified position in this list. Throws  if the specified index is out of range (index = size).
 * Replaces the element at the specified position in this list with the specified element. Throws  if the specified index is is out of range (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
 Different Method in ArrayList
 * Returns  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)).

 Output for Different Method in ArrayList

Question: Consider the following code:

In the example above, what is output?

Some more ArrayList methods: