ArrayList in Java


Standard java arrays size is fixed. Once the arrays are created, they can not grow or shrink which means you can not change the size of the array. You must now the requirement of your program to initialize the array of required size.
To overcome such limitations, java provides the ArrayList class that can store unlimited number of objects. The ArrayList class extends AbstractList and implements the List interface.

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.

Defines an an empty list with the specified initial capacity.

ArrayList supports large number of methods, Few of them are specified in table below.

Inserts the specified element at given position in this list

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

Inserts all of the elements in the specified collection into this list, starting from given position

Removes all of the elements from this list

Returns a shallow copy of this ArrayList

Returns true if this list contains the specified element

Returns the element at the specified position in this list

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

Removes the element at the specified position in this list

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

Replaces the element at the specified position in this list with the specified element

Returns the number of elements in this list

Returns an array containing all of the elements in this list in the correct order

Returns an array containing all of the elements in this list

Example

Output

No Responses