Class CustomArrayList<E>

java.lang.Object
com.collection.arraylist.CustomArrayList<E>
Type Parameters:
E - the type of elements stored in the list
All Implemented Interfaces:
CustomList<E>

public class CustomArrayList<E> extends Object implements CustomList<E>
This class represents a custom implementation of a dynamic array list data structure. It implements the CustomList interface and provides methods to add, remove, get elements, and more. The list automatically expands in capacity when needed to accommodate additional elements.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs an instance of CustomArrayList with the default capacity.
    CustomArrayList(int capacity)
    Constructs an instance of CustomArrayList with the specified capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int index, E element)
    Method to insert the specified element at the specified index in the list.
    boolean
    add(E element)
    Method to add the specified element to the end of the list.
    boolean
    addAll(E[] elements)
    Method to add all the elements in the specified array to the end of the list.
    void
    Removes all of the elements from this list.
    boolean
    contains(E element)
    Method to check if the list contains the specified element.
    void
    ensureCapacity(int minCapacity)
    Method to increase the capacity of this CustomArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
    get(int index)
    Method to get the element at the specified index in the list.
    int
    indexOf(E elem)
    Method which returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
    remove(int index)
    Method to remove the element at the specified index from the list.
    boolean
    remove(E element)
    Method to remove the first occurrence of the specified element from the list.
    set(int index, E element)
    Method to replace the element at the specified index with the specified element.
    int
    Returns the number of elements in the list.
    Method which returns a string representation of this list.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • CustomArrayList

      public CustomArrayList()
      Constructs an instance of CustomArrayList with the default capacity.
    • CustomArrayList

      public CustomArrayList(int capacity)
      Constructs an instance of CustomArrayList with the specified capacity.
      Parameters:
      capacity - the initial capacity of the list
  • Method Details

    • size

      public int size()
      Returns the number of elements in the list.
      Specified by:
      size in interface CustomList<E>
      Returns:
      size - the number of elements in the list
    • get

      public E get(int index)
      Method to get the element at the specified index in the list.
      Specified by:
      get in interface CustomList<E>
      Parameters:
      index - the index of the element to retrieve
      Returns:
      the element at the specified index
    • add

      public boolean add(E element)
      Method to add the specified element to the end of the list.
      Specified by:
      add in interface CustomList<E>
      Parameters:
      element - the element to be added
      Returns:
      true if the element was successfully added, false otherwise
    • add

      public void add(int index, E element)
      Method to insert the specified element at the specified index in the list.
      Specified by:
      add in interface CustomList<E>
      Parameters:
      index - the index at which to insert the element
      element - the element to insert in the list
    • addAll

      public boolean addAll(E[] elements)
      Method to add all the elements in the specified array to the end of the list.
      Specified by:
      addAll in interface CustomList<E>
      Parameters:
      elements - an array of elements to be added
      Returns:
      true if all elements were successfully added, false otherwise
    • remove

      public E remove(int index)
      Method to remove the element at the specified index from the list.
      Specified by:
      remove in interface CustomList<E>
      Parameters:
      index - the index of the element to remove
      Returns:
      the removed element
    • remove

      public boolean remove(E element)
      Method to remove the first occurrence of the specified element from the list.
      Specified by:
      remove in interface CustomList<E>
      Parameters:
      element - the element to be removed
      Returns:
      true if the element was found and removed, false otherwise
    • contains

      public boolean contains(E element)
      Method to check if the list contains the specified element.
      Specified by:
      contains in interface CustomList<E>
      Parameters:
      element - the element to check for
      Returns:
      true if the element is found in the list, false otherwise
    • set

      public E set(int index, E element)
      Method to replace the element at the specified index with the specified element.
      Specified by:
      set in interface CustomList<E>
      Parameters:
      index - the index of the element to replace
      element - the new element to set
      Returns:
      the old element at the specified index
    • indexOf

      public int indexOf(E elem)
      Method which returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
      Specified by:
      indexOf in interface CustomList<E>
      Parameters:
      elem - the element to search for
      Returns:
      the index of the specified element in this list, or -1 if not found
    • clear

      public void clear()
      Removes all of the elements from this list. The list will be empty after this call.
      Specified by:
      clear in interface CustomList<E>
    • toString

      public String toString()
      Method which returns a string representation of this list. The string representation consists of a list of the elements enclosed in square brackets ("[]"), separated by commas and spaces.
      Overrides:
      toString in class Object
      Returns:
      a string representation of this list
    • ensureCapacity

      public void ensureCapacity(int minCapacity)
      Method to increase the capacity of this CustomArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.
      Parameters:
      minCapacity - the desired minimum capacity of this CustomArrayList.