Class List

  • All Implemented Interfaces:
    java.lang.Cloneable
    Direct Known Subclasses:
    ListStack

    public class List
    extends java.lang.Object
    implements java.lang.Cloneable
    This is my implementation of the JDK 1.2 List interface. I wrote this because I want people using 1.1.x to be able to use my apps, but I don't want to use a "synchronized" Vector. I also wanted to get a start in moving my source to JDK 1.2
    I use the implementation of the hashCode method that is listed in the JDK 1.2 API, so this List can be compared correctly to actual JDK 1.2 lists using the equals method. Note: This is not a complete implementation yet, None of the methods that take a Collection have been imlplemented.
    See Also:
    List, Collection
    • Constructor Summary

      Constructors 
      Constructor Description
      List()
      Creates a new BasicSet with the default Size
      List​(int size)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean add​(int index, java.lang.Object obj)
      Adds the specified Object to the list at the specified index
      boolean add​(java.lang.Object obj)
      Adds the specified Object to the list
      void clear()
      Removes all elements from the list
      java.lang.Object clone()  
      boolean contains​(java.lang.Object obj)
      Returns true if the specified element is contained in the list.
      boolean equals​(java.lang.Object obj)
      Compares the specified object with this list for equality.
      java.lang.Object get​(int index)
      Returns the element at the specified position in this list.
      int hashCode()
      As defined by the JDK 1.2 API spec:
      Returns the hash code value for this list.
      int indexOf​(java.lang.Object obj)
      Returns the index of the first occurrence of the specified element, or -1 if the element is not contained in the List
      boolean isEmpty()
      Returns true if there are no elements in the List.
      int lastIndexOf​(java.lang.Object obj)
      Returns the index of the last occurrence of the specified element, or -1 if the element is not contained in the List
      java.lang.Object remove​(int index)
      Removes the element at the specified index from the List
      boolean remove​(java.lang.Object obj)
      Removes the first occurrence of the specified element from the List
      java.lang.Object set​(int index, java.lang.Object element)
      Replaces the element at the specified position in this list with the specified element.
      int size()
      Returns the number of elements in the List
      List subList​(int fromIndex, int toIndex)
      Returns a new List which contains elements from a given section of this list.
      java.lang.Object[] toArray()
      Returns an array containing all of the elements in this list in proper sequence.
      java.lang.Object[] toArray​(java.lang.Object[] dst)
      Returns an array containing all of the elements in this list in proper sequence.
      void trimToSize()
      Reduces the capacity of the internal buffer to the current size freeing up unused memory.
      • Methods inherited from class java.lang.Object

        getClass, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • List

        public List()
        Creates a new BasicSet with the default Size
      • List

        public List​(int size)
    • Method Detail

      • add

        public boolean add​(java.lang.Object obj)
        Adds the specified Object to the list
        Parameters:
        obj - the Object to add to the list
        Returns:
        true if the Object is added to the list
      • add

        public boolean add​(int index,
                           java.lang.Object obj)
                    throws java.lang.IndexOutOfBoundsException
        Adds the specified Object to the list at the specified index
        Parameters:
        obj - the Object to add to the list
        Returns:
        true if the Object is added to the list
        Throws:
        java.lang.IndexOutOfBoundsException
      • clear

        public void clear()
        Removes all elements from the list
      • clone

        public java.lang.Object clone()
      • contains

        public boolean contains​(java.lang.Object obj)
        Returns true if the specified element is contained in the list. if the specfied element is null, then if the list contains a null value, true will be returned.
        Parameters:
        obj - the element to search the list for
        Returns:
        true if specified element is contained in the list
      • equals

        public boolean equals​(java.lang.Object obj)
        Compares the specified object with this list for equality. Returns true if and only if the specified Object is a list and all of its associated elements are equal to the elements of this list
        Overrides:
        equals in class java.lang.Object
      • get

        public java.lang.Object get​(int index)
                             throws java.lang.IndexOutOfBoundsException
        Returns the element at the specified position in this list.
        Parameters:
        index - the position of the element to return
        Throws:
        java.lang.IndexOutOfBoundsException
      • hashCode

        public int hashCode()
        As defined by the JDK 1.2 API spec:
        Returns the hash code value for this list. The hash code of a list is defined to be the result of the following calculation:
        hashCode = 1; Iterator i = list.iterator(); while (i.hasNext()) { Object obj = i.next(); hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode()); }
        Overrides:
        hashCode in class java.lang.Object
        Returns:
        the hash code value for this list
      • indexOf

        public int indexOf​(java.lang.Object obj)
        Returns the index of the first occurrence of the specified element, or -1 if the element is not contained in the List
        Parameters:
        obj - the Object to get the index for
      • isEmpty

        public boolean isEmpty()
        Returns true if there are no elements in the List.
        Returns:
        true if there are no elements in the List.
      • lastIndexOf

        public int lastIndexOf​(java.lang.Object obj)
        Returns the index of the last occurrence of the specified element, or -1 if the element is not contained in the List
        Parameters:
        obj - the Object to get the last index for
      • remove

        public java.lang.Object remove​(int index)
        Removes the element at the specified index from the List
        Parameters:
        index - the position in the list tp remove the element from
        Returns:
        the Object that was removed from the list
      • remove

        public boolean remove​(java.lang.Object obj)
        Removes the first occurrence of the specified element from the List
        Parameters:
        obj - the Object to remove from the List
        Returns:
        true if the Object was removed from the list
      • trimToSize

        public void trimToSize()
        Reduces the capacity of the internal buffer to the current size freeing up unused memory.
      • set

        public java.lang.Object set​(int index,
                                    java.lang.Object element)
                             throws java.lang.IndexOutOfBoundsException
        Replaces the element at the specified position in this list with the specified element.
        Parameters:
        index - the position in the list to place the element at
        element - the element to add to the list
        Throws:
        java.lang.IndexOutOfBoundsException
      • size

        public int size()
        Returns the number of elements in the List
        Returns:
        the number of elements in the List
      • subList

        public List subList​(int fromIndex,
                            int toIndex)
        Returns a new List which contains elements from a given section of this list.
        Parameters:
        fromIndex - the start index (inclusize) of elements to add to the new list
        toIndex - the end index (exclusive)of the elements to add to the new list
        Returns:
        a new List which contains elements from a given section of this list.
        Throws:
        java.lang.IndexOutOfBoundsException - for invalid index values
      • toArray

        public java.lang.Object[] toArray()
        Returns an array containing all of the elements in this list in proper sequence.
        Returns:
        the array of elements of this List
      • toArray

        public java.lang.Object[] toArray​(java.lang.Object[] dst)
        Returns an array containing all of the elements in this list in proper sequence.
        Returns:
        the array of elements of this List