Class DoubleList

java.lang.Object
org.cicirello.util.DoubleList
All Implemented Interfaces:
Copyable<DoubleList>

public final class DoubleList extends Object implements Copyable<DoubleList>
This class is an implementation of a partially-filled array of primitive double values.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    The default initial capacity of the list.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Initializes an empty DoubleList.
    DoubleList(double[] initialContents)
    Initializes an DoubleList from an array,
    DoubleList(int initialCapacity)
    Initializes an empty DoubleList, with a specified initial capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(double element)
    Adds a new element to the end of the list, growing the internal array if necessary.
    void
    add(int index, double element)
    Inserts a new element into the list, growing the internal array if necessary.
    void
    Logically clears the list.
    boolean
    contains(double element)
    Checks if the list contains a value.
    Creates an identical copy of this object.
    void
    ensureCapacity(int minCapacity)
    Increases capacity if necessary to achieve a specified minimum capacity.
    boolean
    equals(Object other)
    Checks if this list is equal to another list, such that they are the same size, and contain all of the same elements in the same order.
    double
    get(int index)
    Gets the element at a specified index.
    int
    Computes a hashCode for the list.
    int
    indexOf(double element)
    Gets the index of the first occurrence of an element from the left.
    boolean
    Checks if the list is empty.
    int
    lastIndexOf(double element)
    Gets the index of the last occurrence of an element.
    double
    remove(int index)
    Removes the element at a specified index.
    void
    set(int index, double element)
    Sets an element at a specified index, replacing any value that is currently there.
    int
    Gets the size of the list.
    void
    Sorts list into ascending order.
    double[]
    Returns an array containing all of the element from the list.
    Generate a String representation of the list.
    void
    Reduces the capacity of the list to the current value of size().

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • DEFAULT_INITIAL_CAPACITY

      public static final int DEFAULT_INITIAL_CAPACITY
      The default initial capacity of the list. The capacity will grow as needed.
      See Also:
  • Constructor Details

    • DoubleList

      public DoubleList()
      Initializes an empty DoubleList.
    • DoubleList

      public DoubleList(int initialCapacity)
      Initializes an empty DoubleList, with a specified initial capacity. This may be useful if you anticipate adding a large number of elements as it can help minimize the number of array reallocations.
      Parameters:
      initialCapacity - The initial capacity for the buffer array holding the contents.
      Throws:
      IllegalArgumentException - if initialCapacity is less than 1.
    • DoubleList

      public DoubleList(double[] initialContents)
      Initializes an DoubleList from an array,
      Parameters:
      initialContents - The initial contents of the list, which must contain at least one element. This constructor clones this array so it is free of unintended side-effects.
      Throws:
      IllegalArgumentException - if initialContents.length is less than one.
  • Method Details

    • add

      public void add(double element)
      Adds a new element to the end of the list, growing the internal array if necessary.
      Parameters:
      element - The value to add.
    • add

      public void add(int index, double element)
      Inserts a new element into the list, growing the internal array if necessary.
      Parameters:
      index - The index for the insertion.
      element - The value to add.
      Throws:
      IndexOutOfBoundsException - if the index is out of bounds, such that index is negative or index is greater than size().
    • clear

      public void clear()
      Logically clears the list.
    • contains

      public boolean contains(double element)
      Checks if the list contains a value.
      Parameters:
      element - The element to search for.
      Returns:
      true if and only if the list contains at least one copy of element.
    • copy

      public DoubleList copy()
      Description copied from interface: Copyable
      Creates an identical copy of this object.
      Specified by:
      copy in interface Copyable<DoubleList>
      Returns:
      an identical copy of this object.
    • ensureCapacity

      public void ensureCapacity(int minCapacity)
      Increases capacity if necessary to achieve a specified minimum capacity. If increased, the new capacity may or may not exactly equal to requested minimum. The only guarantee is that it is at least as large as requested.
      Parameters:
      minCapacity - The minimum capacity for the list.
    • get

      public double get(int index)
      Gets the element at a specified index.
      Parameters:
      index - The index of the desired element.
      Returns:
      The element at the specified index.
      Throws:
      IndexOutOfBoundsException - if the index is out of bounds, such that index is negative or index is greater than or equal to size().
    • indexOf

      public int indexOf(double element)
      Gets the index of the first occurrence of an element from the left.
      Parameters:
      element - The element searched for.
      Returns:
      The index of the first occurrence of the element in the list from the left, or -1 if the element doesn't exist.
    • isEmpty

      public boolean isEmpty()
      Checks if the list is empty.
      Returns:
      true if and only if size() equals 0.
    • lastIndexOf

      public int lastIndexOf(double element)
      Gets the index of the last occurrence of an element.
      Parameters:
      element - The element searched for.
      Returns:
      The index of the last occurrence of the element in the list, or -1 if the element doesn't exist.
    • remove

      public double remove(int index)
      Removes the element at a specified index.
      Parameters:
      index - The index of the element to remove.
      Returns:
      The element that was removed.
      Throws:
      IndexOutOfBoundsException - if the index is out of bounds, such that index is negative or index is greater than or equal to size().
    • set

      public void set(int index, double element)
      Sets an element at a specified index, replacing any value that is currently there.
      Parameters:
      index - The index.
      element - The new element to set at that index.
      Throws:
      IndexOutOfBoundsException - if the index is out of bounds, such that index is negative or index is greater than or equal to size().
    • size

      public int size()
      Gets the size of the list.
      Returns:
      The size of the list, which is the number of elements currently within it.
    • sort

      public void sort()
      Sorts list into ascending order.
    • toArray

      public double[] toArray()
      Returns an array containing all of the element from the list. This method constructs a new array with length equal to the current size() of the list. This method does not expose the encapsulated array.
      Returns:
      An array containing all of the elements currently in the list.
    • trimToSize

      public void trimToSize()
      Reduces the capacity of the list to the current value of size(). However, if size() is 0, this method will trim the list capacity to 1. If you don't anticipate adding additional elements to the list, and if your application is memory constrained, then this method can save memory.
    • equals

      public boolean equals(Object other)
      Checks if this list is equal to another list, such that they are the same size, and contain all of the same elements in the same order.
      Overrides:
      equals in class Object
      Parameters:
      other - The other list
      Returns:
      true If the lists are the same size and contain the same elements in the same order.
    • hashCode

      public int hashCode()
      Computes a hashCode for the list.
      Overrides:
      hashCode in class Object
      Returns:
      a hashCode
    • toString

      public String toString()
      Generate a String representation of the list.
      Overrides:
      toString in class Object
      Returns:
      A String representation of the list.