ArrayList is a resizable-array of the list interface. It provides the methods that can be used to manipulate the size of the array whenever required.
Each instance of an ArrayList has some capacity, the capacity means the size to store the elements in the ArrayList. If you do not assign any capacity then the default capacity is 10, but if you enter the 11th element then it will automatically increase the capacity of the array as per your requirement since the ArrayList list is a growable list.
Some operations of ArrayList such as size(), isEmpty(), get(), set(), iterator(), and ListIterator() run in constant time. The add operation runs in n time, n is the number of times you are calling the add method or the number of times you are adding the element in the array.
Constructors of ArrayList:
- ArrayList() : Creates an empty ArrayList initially.
- ArrayList(Collection <? extends E> c) : Creates an ArrayList that contains the elements of the given collection.
Implementation of ArrayList API:
Contains 120? : true Contains 200? : false Elements of the array using enhanced for-loop : 100 120 500 220 150 Elements of the array using iterator : 100 120 500 220 150 Elements of array using list - iterator : 100 120 500 220 150 Element at index 2 is : 500 Element 500 is at index position : 2 Array is empty? false Last index of the element 100 is : 0 Remove the element which is at index 3 : 220 Array element after filtering : [100, 500, 220, 233] Retain the array element : [100, 500, 220, 233] Size of the array is : 4 Array elements after sorting : [100, 220, 500, 999] Ordered Sized Estimate size of arraylist is : 4 Exact size of array is : 4 500 999 ***** 100 220 Sub-list from the index position 1 to 3 : [220, 500] ArrayList traversal using toArray method : [100, 220, 500, 999] ArrayList traversal using toArray with argument method : [100, 220, 500, 999] Array size after trimming : 4
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.