Introduction to ArrayList

What is ArrayList?

ArrayList is a dynamic data structure from the Java Collection Framework. It is a widely used data structure in Java developer life. For example, as a developer you are required to fulfill the requirements such as “collecting items into cart”, “delete multiple emails from your inbox”, “multiple payment options for a single transaction”, “multiple products are displayed at a time” etc. These are the functionality which we need to fulfill but we don’t know the quantity. So, we need a data structure that grows dynamically without human/developer involvement.

Java collection offers different options to fulfill our requirements like ArrayList, LinkedList, HashMap, TreeMap, HashSet, etc. All are dynamic in nature. The developer doesn’t need to provide any size as we do in Array-based operations. ArrayList increases its size whenever a new item is added if required. This will be taken care of by ArrayList itself.

ArrayList backed by the array data structure and array expected an integer value to initialize. So, it’s the reason for the initial default capacity is 10 for ArrayList when we don’ provide size/capacity. ArrayList can also be initialized with some size as per your choice but if you don’t provide then it takes 10 as a default capacity/size. Check below example:

1. The most common way to initialize the ArrayList using default constructor:

List<String> list = new ArrayList<String>();

2. The another way with some initial capacity using constructor argument:

List<String> list = new ArrayList<String>(20);

Most important part: Time complexity:
Add, Remove operation take O(1) time.
Shifting of values operation take O(n) time.

Leave a Reply

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