ArrayList is same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.
- ArrayList elements are placed in contiguous storage so that they can be accessed and traversed using iterators.
- In ArrayList, data is inserted at the end.
- Inserting at the end takes differential time, as sometimes there may be a need of extending the array.
- Removing the last element takes only constant time because no resizing happens.
- Inserting and erasing at the beginning or in the middle is linear in time.
Functions to be implemented in the Dynamic array class:
Certain functions associated with the ArrayList that we will implement are:
- void push(int data): This function takes one element and inserts it at the last. Amortized time complexity is O(1).
- void push(int data, int index): It inserts data at the specified index. Time complexity is O(1).
- int get(int index): It is used to get the element at the specified index. Time complexity is O(1).
- void pop(): It deletes the last element. Time complexity is O(1).
- int size(): It returns the size of the ArrayList i.e, number of elements in the ArrayList. Time complexity is O(1).
- int getcapacity(): It returns the capacity of the ArrayList. Time complexity is O(1).
- void print(): It is used to print array elements. Time complexity is O(N), where N is the size of the ArrayList.
Implementation of the Dynamic array class:
Below is the implementation of our own ArrayList class.
ArrayList size: 5 ArrayList capacity: 8 ArrayList elements: 10 20 30 40 50 After updating 1st index ArrayList elements: 10 100 30 40 50 Element at 1st index: 100 After deleting the last element ArrayList size: 4 ArrayList capacity: 8 ArrayList elements: 10 100 30 40
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Implement Sextet Class from Quintet Class in Java using JavaTuples
- Implement Ennead Class from Octet Class in Java using JavaTuples
- Implement Quintet Class with Quartet Class in Java using JavaTuples
- Implement Triplet Class with Pair Class in Java using JavaTuples
- Implement Octet Class from Septet Class in Java using JavaTuples
- Implement Quartet Class with Triplet Class in Java using JavaTuples
- Implement Pair Class with Unit Class in Java using JavaTuples
- Implement Decade Class from Ennead Class in Java using JavaTuples
- Implement Septet Class from Sextet Class in Java using JavaTuples
- How to implement our own Vector Class in C++?
- Reflection Array Class in Java
- Implement two stacks in an array
- How to implement SQL GROUP BY in Java?
- In Java, Can we call the main() method of a class from another class?
- Difference between Abstract Class and Concrete Class in Java
- How to implement an Interface using an Enum in Java
- Implement Runnable vs Extend Thread in Java
- Using predefined class name as Class or Variable name in Java
- Java.lang.Class class in Java | Set 2
- Java.lang.Class class in Java | Set 1
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.