Class MyLinkedList<E>

  • All Implemented Interfaces:
    MyList<E>

    public class MyLinkedList<E>
    extends java.lang.Object
    implements MyList<E>
    A class that implements the MyList interface using a linked list of nodes.
    Version:
    1.1
    Author:
    Rui Meireles
    • Constructor Summary

      Constructors 
      Constructor Description
      MyLinkedList()
      List constructor.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(int idx, E elem)
      Adds a new element to the list at the index specified as an argument.
      boolean add​(E elem)
      Appends a new element to the end of the list.
      E get​(int idx)
      Returns the list element stored at the index specified as an argument.
      int indexOf​(E elem)
      Returns the index at which the element passed as an argument can be found on the list.
      E remove​(int idx)
      Removes and returns the list element located at the index specified as an argument.
      int size()
      Returns the number of elements on the list.
      java.lang.String toString()
      Returns a textual representation of the list elements in the format "{elem1, elem2, .., elemn}".
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • MyLinkedList

        public MyLinkedList()
        List constructor.
    • Method Detail

      • size

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

        public boolean add​(E elem)
        Appends a new element to the end of the list.
        Specified by:
        add in interface MyList<E>
        Parameters:
        elem - the element to add to the list.
        Returns:
        always returns true.
      • add

        public void add​(int idx,
                        E elem)
                 throws java.lang.IndexOutOfBoundsException
        Adds a new element to the list at the index specified as an argument.
        Specified by:
        add in interface MyList<E>
        Parameters:
        idx - the index at which the new element should be found once it has been added to the list.
        elem - the element to add to the list.
        Throws:
        java.lang.IndexOutOfBoundsException - if index not in range [0,size()].
      • remove

        public E remove​(int idx)
                 throws java.lang.IndexOutOfBoundsException
        Removes and returns the list element located at the index specified as an argument.
        Specified by:
        remove in interface MyList<E>
        Parameters:
        idx - the index at which the element to be removed resides.
        Returns:
        the element removed.
        Throws:
        java.lang.IndexOutOfBoundsException
      • get

        public E get​(int idx)
              throws java.lang.IndexOutOfBoundsException
        Returns the list element stored at the index specified as an argument.
        Specified by:
        get in interface MyList<E>
        Parameters:
        idx - the index of the element to be retrieved.
        Returns:
        the element at the specified index.
        Throws:
        java.lang.IndexOutOfBoundsException
      • indexOf

        public int indexOf​(E elem)
        Returns the index at which the element passed as an argument can be found on the list. Equality is determined by the equals(Object o) method, defined in the Object class, and potentially overridden by the element's class. If the elem appears multiple times on the list, the lowest index at which it can be found is returned. If the element is not present in the list, -1 is returned.
        Specified by:
        indexOf in interface MyList<E>
        Parameters:
        elem - the element we're trying to find.
        Returns:
        the index of the first ocurrence of elemement elem, or -1 if not found.
      • toString

        public java.lang.String toString()
        Returns a textual representation of the list elements in the format "{elem1, elem2, .., elemn}". E.g. A list with elements 1, 2 and 3 should return "{1, 2, 3}".
        Specified by:
        toString in interface MyList<E>
        Overrides:
        toString in class java.lang.Object
        Returns:
        the string representation of the list.