ArrayList in Java
ArrayList:
- ArrayList is resizable-array implementation of the List interface.
- It implements all optional list operations, and permits all elements, including null.
- This class provides methods to manipulate to the size of array that is used internally to store the list. (This class is roughly equivalent to Vector, except that it is unsynchronized.)
- Java ArrayList class can contain duplicate elements.
- Java ArrayList class maintains insertion order.
- Java ArrayList allows random access because array works at the index basis.
- Manipulation in ArrayList is slow because a lot of shifting needs to be done if any element is added or remove in arraylist.
Let’s understand how to use ArrayList with examples.
ArrayList Basics:
- Initialize the ArrayList
- Example of using ArrayList
- Find size of ArrayList
- Iterate over ArrayList Elements
Add and Remove elements:
- Add Elements to ArrayList
- Add element to ArrayList at a particular index
- Add all collections in ArrayList
- Add a collection of elements at a particular index in ArrayList
- Remove a specified element from the array List
- Remove the element from the specified index in ArrayList
Sort ArrayList:
- Sort ArrayList
- Sort the ArrayList in descending order
- Sort ArrayList of Objects using Comparable and Comparator interface
Other Useful Functions on ArrayList:
- Find subList of ArayList
- Get index of first and last occurrence of element in arrayList
- Compare two arrayLists
- Swap two elements in arrayList
- Replace the value of existing element in arrayList