Java ArrayList

The ArrayList in java is Resizable-array implementation of the List  interface for storing group of elements, which can be found in the java.util package.

  1.  ArrayList in java allows duplicate values to store.
  2.  ArrayListallows null values to store.
  3. ArrayList preserves insertion order.
  4. The underlying data structure is Resizable-array (or) grow-able array.

ArrayList Java

Creating ArrayList in Java :

Constructors in  ArrayList class to create ArrayList objects.

  1.  ArrayList() ArrayList():  is used to build an empty ArrayList object.
  2.  ArrayList(Collection c): is used to build an ArrayList initialized with the elements from any other Collection c
  3.  ArrayList(int capacity): is used to build an ArrayList with initial capacity being specified
Copy to Clipboard

Adding elements to ArrayList

Copy to Clipboard

Output:

Copy to Clipboard

Java ArrayList methods

  1. boolean add(Object o): is used to append a specified element to the end of a list.
  2. void add(int index, Object element): is used to insert a specific element at a specific position index in a list.
  3. boolean addAll(int index, Collection C): is used to insert all of the elements starting at the specified position from a specific collection into the mentioned list.
  4. boolean addAll(Collection C):  is used to append all the elements from a specific collection to the end of the mentioned list, in such a order that the values are returned by the specified collection’s iterator.
  5. void clear():  is used to remove all the elements from any list.
  6. contains​(Object o): returns true if this list contains the specified element otherwise false.
  7. get​(int index): returns the element at the specified position in this list.
  8. int indexOf(Object O): is returns the index of the first occurrence of a specific element, or return -1 in case the element is not in the list.
  9. int lastIndexOf(Object O): returns the index of the last occurrence of a specific element, or return -1 in case the element is not in the list.
  10. forEach​(Consumer<? super E> action): performs the given action for each element of the Iterable until all elements have been processed or the action throws an exception.
  11. isEmpty​(): returns true if this list contains no elements.
  12. retainAll​(Collection<?> c): retains only the elements in this list that are contained in the specified collection.
  13. removeIf​(Predicate<? super E> filter): removes all of the elements of this collection that satisfy the given predicate.
  14. remove​(int index): removes the element at the specified position in this list.
  15. remove​(Object o): removes the first occurrence of the specified element from this list, if it is present.
  16. removeAll​(Collection<?> c): removes from this list all of its elements that are contained in the specified collection.
  17. spliterator​(): creates a late-binding and fail-fast Spliterator over the elements in this list.
  18. set​(int index, E element): replaces the element at the specified position in this list with the specified element.
  19. size​(): Returns the number of elements in this list.
  20. ensureCapacity​(int minCapacity): Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
  21. subList​(int fromIndex, int toIndex): returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
  22. listIterator​(): returns a list iterator over the elements in this list (in proper sequence).
  23. listIterator​(int index): returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
  24. removeRange​(int fromIndex, int toIndex): removes from this list all of the elements whose index is between fromIndex, inclusive, and toIndex, exclusive.
  25. void trimToSize():  is used to trim the capacity of the instance of the ArrayLis to the list’s current size.
  26. Object clone():  is used to return a shallow copy of an ArrayList.
  27. Object[] toArray(): is used to return an array containing all of the elements in the list in correct order.
  28. Object[] toArray(Object[] O):  is used to return an array containing all of the elements in this list in the correct order same as the previous method.

Java  ArrayList methods examples

Copy to Clipboard

Output results

Copy to Clipboard

References