ArrayList in Java


 A normal array is where we give a size and then add values to it to the size of the array. If you want to add more values than the size, you need to create a new array. 

To avoid this disadvantage, we use ArrayList. If you add values more than the size of the array, the size of the array will automatically increase the size. And when you remove a value, it automatically decreases the size. Let's now see how an ArrayList works with an example.


Example Code 




point-by-point list of common ArrayList methods in Java, along with short description

 Basic Methods
  • add(E e) – Adds an element to the end of the list.
  • add(int index, E element) – Inserts an element at a specific position.
  • get(int index) – Returns the element at the specified index.
  • set(int index, E element) – Replaces the element at a given index.
  • remove(int index) – Removes the element at the specified index.
  • remove(Object o) – Removes the first occurrence of the specified object.
  • clear() – Removes all elements from the list.

Size and Status
  • size() – Returns the number of elements in the list.
  • isEmpty() – Checks if the list is empty.
  • contains(Object o) – Returns true if the list contains the specified element.

Search Operations
  • indexOf(Object o) – Returns the index of the first occurrence of an element.
  • lastIndexOf(Object o) – Returns the index of the last occurrence of an element.

Iteration Methods
  • iterator() – Returns an iterator to traverse the list.
  • listIterator() – Returns a list iterator (can traverse forward and backward).
  • listIterator(int index) – Returns a list iterator starting from a given index.
  • forEach(Consumer<? super E> action) – Performs an action for each element.

Bulk Operations
  • addAll(Collection<? extends E> c) – Adds all elements from another collection.
  • addAll(int index, Collection<? extends E> c) – Inserts all elements at a specific index.
  • removeAll(Collection<?> c) – Removes all elements found in the given collection.
  • retainAll(Collection<?> c) – Keeps only elements that exist in the given collection.
  • subList(int fromIndex, int toIndex) – Returns a portion (view) of the list.

Conversion and Array Handling
  • toArray() – Converts the list into an array of Object.
  • toArray(T[] a) – Converts the list into an array of a specific type.
  • clone() – Returns a shallow copy of the ArrayList.

Other Useful Methods
  • ensureCapacity(int minCapacity) – Increases capacity if needed (improves performance).
  • trimToSize() – Trims the capacity to the current list size (saves memory).
  • removeIf(Predicate<? super E> filter) – Removes elements that satisfy a given condition.
  • replaceAll(UnaryOperator<E> operator) – Replaces each element with the result of a function.
  • sort(Comparator<? super E> c) – Sorts the list according to the specified comparator.




Comments

Popular posts from this blog

Java Design Patterns

Application Programming Interface (API)