ArrayList is a collection which presents in java.util package. It is also known as dynamic array.

ArrayList Inheritance

The problem we are facing with array is it has fixed length. We cannot increase the length of the array once define. ArrayList is dynamic with length. It can increase the length as per addition in element.

Java ArrayList uses an array as the internal programming construct to store elements. An array is nothing but a sequential collection same type of elements, accessed by their index values.

Common operations are add/remove elements in various ways, determine if the list is empty, obtain the size of the list indicated by the number of elements present currently, and so forth.

We can declare ArrayList in below syntax :

Initial Default Capacity/Size/Length for ArrayList is 10.

Available Constructors for ArrayList :

  • ArrayList() : Constructs an empty list with an initial storage capacity of ten elements.
  • ArrayList(int initialCapacity) : Constructs an empty list, but we can override the default initial capacity.
  • ArrayList(Collection c) : Constructs a list containing elements in a pre-specified collection.
Generic ArrayList

Java ArrayList is built to support generic types. That is the reason that the class refers to having elements of type E, such as ArrayList. The E is replaced by the type of element such as String, Employee, and so on. There is a subtle difference between a normal array and Java ArrayList: Here we do not create an ArrayList of objects, rather we create an ArrayList object that stores a particular object type. For example, if we want to create an ArrayList of String object type, we may write:

Similarly, for Employee object, we may write:

We also may create an ArrayList without specifying a type, such as:

This, however, is not always a good practice because we should let the compiler know the type of data element prior to adding it to the list. The ? denotes a wild card.

Formula to grow capacity of ArrayList :

There were lots of improvements in Java 8 regarding collection framework.

Leave a Reply

Your email address will not be published. Required fields are marked *