Class ArrayList<E>

java.lang.Object
com.zybooks.dsaj.list.ArrayList<E>
Type Parameters:
E - the element type
All Implemented Interfaces:
List<E>, Iterable<E>

public class ArrayList<E> extends Object implements List<E>
Realization of a list by means of a dynamic array. This is a simplified version of the java.util.ArrayList class.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Default array capacity.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Creates an array list with default initial capacity.
    ArrayList(int capacity)
    Creates an array list with given initial capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int i, E e)
    Inserts the given element at the specified index of the list, shifting all subsequent elements in the list one position further to make room.
    protected void
    checkIndex(int i, int n)
    Checks whether the given index is in the range [0, n-1].
    get(int i)
    Returns (but does not remove) the element at index i.
    boolean
    Tests whether the array list is empty.
    Returns an iterator of the elements stored in the list.
    remove(int i)
    Removes and returns the element at the given index, shifting all subsequent elements in the list one position closer to the front.
    protected void
    resize(int capacity)
    Resizes the internal array to have given capacity >= size.
    set(int i, E e)
    Replaces the element at the specified index, and returns the element previously stored.
    int
    Returns the number of elements in the list.
    Produces a string representation of the contents of the indexed list.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait

    Methods inherited from interface java.lang.Iterable

    forEach, spliterator
  • Field Details

  • Constructor Details

    • ArrayList

      public ArrayList()
      Creates an array list with default initial capacity.
    • ArrayList

      public ArrayList(int capacity)
      Creates an array list with given initial capacity.
      Parameters:
      capacity - the initial capacity of the dynamic array
  • Method Details

    • size

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

      public boolean isEmpty()
      Tests whether the array list is empty.
      Specified by:
      isEmpty in interface List<E>
      Returns:
      true if the array list is empty, false otherwise
    • get

      public E get(int i) throws IndexOutOfBoundsException
      Returns (but does not remove) the element at index i.
      Specified by:
      get in interface List<E>
      Parameters:
      i - the index of the element to return
      Returns:
      the element at the specified index
      Throws:
      IndexOutOfBoundsException - if the index is negative or greater than size()-1
    • set

      public E set(int i, E e) throws IndexOutOfBoundsException
      Replaces the element at the specified index, and returns the element previously stored.
      Specified by:
      set in interface List<E>
      Parameters:
      i - the index of the element to replace
      e - the new element to be stored
      Returns:
      the previously stored element
      Throws:
      IndexOutOfBoundsException - if the index is negative or greater than size()-1
    • add

      public void add(int i, E e) throws IndexOutOfBoundsException
      Inserts the given element at the specified index of the list, shifting all subsequent elements in the list one position further to make room.
      Specified by:
      add in interface List<E>
      Parameters:
      i - the index at which the new element should be stored
      e - the new element to be stored
      Throws:
      IndexOutOfBoundsException - if the index is negative or greater than size()
    • remove

      public E remove(int i) throws IndexOutOfBoundsException
      Removes and returns the element at the given index, shifting all subsequent elements in the list one position closer to the front.
      Specified by:
      remove in interface List<E>
      Parameters:
      i - the index of the element to be removed
      Returns:
      the element that had be stored at the given index
      Throws:
      IndexOutOfBoundsException - if the index is negative or greater than size()
    • checkIndex

      protected void checkIndex(int i, int n) throws IndexOutOfBoundsException
      Checks whether the given index is in the range [0, n-1].
      Parameters:
      i - the given index
      n - one greater than the maximum allowable index
      Throws:
      IndexOutOfBoundsException - if the index is negative or greater than n
    • resize

      protected void resize(int capacity)
      Resizes the internal array to have given capacity >= size.
      Parameters:
      capacity - the new capacity
    • iterator

      public Iterator<E> iterator()
      Returns an iterator of the elements stored in the list.
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface List<E>
      Returns:
      iterator of the list's elements
    • toString

      public String toString()
      Produces a string representation of the contents of the indexed list. This exists for debugging purposes only.
      Overrides:
      toString in class Object
      Returns:
      textual representation of the array list