Package utilities

Class MyArrayList<E>

java.lang.Object
utilities.MyArrayList<E>
Type Parameters:
E - The type of elements in this list.
All Implemented Interfaces:
Serializable, ListADT<E>, StackADT<E>

public class MyArrayList<E> extends Object implements ListADT<E>, StackADT<E>
A custom List implementation.
See Also:
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private static class 
    A custom Iterator implementation for MyArrayList.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private static final int
     
    private Object[]
     
    private int
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    Initializes a new instance of MyArrayList.
    MyArrayList(int capacity)
    Initializes a new instance of MyArrayList.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(int index, E toAdd)
    Inserts the specified element at the specified position in this list.
    boolean
    add(E toAdd)
    Appends the specified element to the end of this list.
    boolean
    addAll(ListADT<? extends E> toAdd)
    Appends all the elements in the specified java.utilCollection to the end of this list, in the order that they are returned by the specified collection's Iterator.
    void
    Removes all of the elements from this list.
    boolean
    contains(E toFind)
    Returns true if this list contains the specified element.
    get(int index)
    Returns the element at the specified position in this list.
    private void
    A helper method that doubles the list size.
    boolean
    Returns true if this list contains no elements.
    Returns an iterator over the elements in this list, in a proper sequence.
    Returns the data of the element to the last position
    pop()
    Removes the element in the last position
    void
    push(E element)
    Returns the data of the element to the last position
    remove(int index)
    Removes the element at the specified position in this list.
    remove(E toRemove)
    Removes the first occurrence in this list of the specified element.
    set(int index, E toChange)
    Replaces the element at the specified position in this list with the specified element.
    int
    The size method will return the current element count contained in the list.
    Returns an array containing all the elements in this list in a proper sequence.
    E[]
    toArray(E[] toHold)
    Returns an array containing all of the elements in this list in a proper sequence; the runtime type of the returned array is that of the specified array.

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_VALUE

      private static final int DEFAULT_VALUE
      See Also:
    • elements

      private Object[] elements
    • size

      private int size
  • Constructor Details

    • MyArrayList

      public MyArrayList()
      Initializes a new instance of MyArrayList.
    • MyArrayList

      public MyArrayList(int capacity)
      Initializes a new instance of MyArrayList.
      Parameters:
      capacity - The initial capacity for this list.
  • Method Details

    • grow

      private void grow()
      A helper method that doubles the list size.
    • size

      public int size()
      Description copied from interface: ListADT
      The size method will return the current element count contained in the list.
      Specified by:
      size in interface ListADT<E>
      Returns:
      The current element count.
    • clear

      public void clear()
      Description copied from interface: ListADT
      Removes all of the elements from this list. This list will be empty after this call returns.
      Specified by:
      clear in interface ListADT<E>
    • add

      public boolean add(int index, E toAdd) throws NullPointerException, IndexOutOfBoundsException
      Description copied from interface: ListADT
      Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).
      Specified by:
      add in interface ListADT<E>
      Parameters:
      index - The index at which the specified element is to be inserted. The element is inserted before the existing element at [index], or at the end if index is equal to the size (size()).
      toAdd - The element to be inserted.
      Returns:
      true if the element is added successfully.
      Throws:
      NullPointerException - If the specified element is null and the list implementation does not support having null elements.
      IndexOutOfBoundsException - If the index is out of range:
      i.e. (index < 0 || index >= size()).
    • add

      public boolean add(E toAdd) throws NullPointerException, IndexOutOfBoundsException
      Description copied from interface: ListADT
      Appends the specified element to the end of this list. Implementations that support this operation may place limitations on what elements may be added to this list. In particular, some implementations will refuse to add null elements. List classes should clearly specify in their documentation any restrictions on what elements may be added.
      Specified by:
      add in interface ListADT<E>
      Parameters:
      toAdd - Element to be appended to this list.
      Returns:
      true if the element appended successfully.
      Throws:
      NullPointerException - If the specified element is null and the list implementation does not support having null elements.
      IndexOutOfBoundsException - If the index is out of range:
      i.e. (index < 0 || index >= size()).
    • addAll

      public boolean addAll(ListADT<? extends E> toAdd) throws NullPointerException
      Description copied from interface: ListADT
      Appends all the elements in the specified java.utilCollection to the end of this list, in the order that they are returned by the specified collection's Iterator. The behaviour of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)
      Specified by:
      addAll in interface ListADT<E>
      Parameters:
      toAdd - The new sub list to be added.
      Returns:
      true If the operation is successful.
      Throws:
      NullPointerException - If the specified element is null and the list implementation does not support having null elements.
    • get

      public E get(int index) throws IndexOutOfBoundsException
      Description copied from interface: ListADT
      Returns the element at the specified position in this list.
      Specified by:
      get in interface ListADT<E>
      Parameters:
      index - Index of the element to return.
      Returns:
      The element at the specified position in this list.
      Throws:
      IndexOutOfBoundsException - If the index is out of range:
      i.e. (index < 0 || index >= size()).
    • remove

      public E remove(int index) throws IndexOutOfBoundsException
      Description copied from interface: ListADT
      Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.
      Specified by:
      remove in interface ListADT<E>
      Parameters:
      index - The index of the element to remove.
      Returns:
      The removed element.
      Throws:
      IndexOutOfBoundsException - If the index is out of range:
      i.e. (index < 0 || index >= size()).
    • remove

      public E remove(E toRemove) throws NullPointerException
      Description copied from interface: ListADT
      Removes the first occurrence in this list of the specified element. If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that o.equals(get(i)) (if such an element exists).
      Specified by:
      remove in interface ListADT<E>
      Parameters:
      toRemove - The element to be removed from this list.
      Returns:
      The element which is being removed, or null if the list does not contain the element.
      Throws:
      NullPointerException - If the specified element is null and the list implementation does not support having null elements.
    • set

      public E set(int index, E toChange) throws NullPointerException, IndexOutOfBoundsException
      Description copied from interface: ListADT
      Replaces the element at the specified position in this list with the specified element.
      Specified by:
      set in interface ListADT<E>
      Parameters:
      index - The index of the element to replace.
      toChange - Element to be stored at the specified position.
      Returns:
      The element previously at the specified position.
      Throws:
      NullPointerException - If the specified element is null and the list implementation does not support having null elements.
      IndexOutOfBoundsException - If the index is out of range:
      i.e. (index < 0 || index >= size()).
    • isEmpty

      public boolean isEmpty()
      Description copied from interface: ListADT
      Returns true if this list contains no elements.
      Specified by:
      isEmpty in interface ListADT<E>
      Returns:
      true if this list contains no elements.
    • contains

      public boolean contains(E toFind) throws NullPointerException
      Description copied from interface: ListADT
      Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that toFind.equals(e).
      Specified by:
      contains in interface ListADT<E>
      Parameters:
      toFind - The element whose presence in this list is to be tested.
      Returns:
      true if this list contains the specified element.
      Throws:
      NullPointerException - If the specified element is null and the list implementation does not support having null elements.
    • toArray

      public E[] toArray(E[] toHold) throws NullPointerException
      Description copied from interface: ListADT
      Returns an array containing all of the elements in this list in a proper sequence; the runtime type of the returned array is that of the specified array. Obeys the general contract of the java.util.Collection.toArray(Object []) method.
      Specified by:
      toArray in interface ListADT<E>
      Parameters:
      toHold - The array into which the elements of this list are to be stored if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.
      Returns:
      An array containing the elements of this list.
      Throws:
      NullPointerException - If the specified array is null.
    • toArray

      public Object[] toArray()
      Description copied from interface: ListADT
      Returns an array containing all the elements in this list in a proper sequence. Obeys the general contract of the java.util.Collection.toArray() method.
      Specified by:
      toArray in interface ListADT<E>
      Returns:
      An array containing all the elements in this list in a proper sequence.
    • iterator

      public Iterator<E> iterator()
      Description copied from interface: ListADT
      Returns an iterator over the elements in this list, in a proper sequence.
      Specified by:
      iterator in interface ListADT<E>
      Returns:
      An iterator over the elements in this list, in a proper sequence. NB: The return is of type linearUtilities.Iterator<E>, not java.util.Iterator.
    • push

      public void push(E element) throws NullPointerException
      Description copied from interface: StackADT
      Returns the data of the element to the last position
      Specified by:
      push in interface StackADT<E>
      Parameters:
      element - The element to be pushed onto the stack
      Throws:
      NullPointerException - If the specified element is null
    • pop

      public E pop() throws NoSuchElementException
      Removes the element in the last position
      Specified by:
      pop in interface StackADT<E>
      Returns:
      E Runs the remove method on the last element
      Throws:
      NoSuchElementException - If this stack is empty
    • peek

      public E peek() throws NoSuchElementException
      Returns the data of the element to the last position
      Specified by:
      peek in interface StackADT<E>
      Returns:
      Gets the data stored in the last index.
      Throws:
      NoSuchElementException - If this stack is empty