Class DoubleArray

java.lang.Object
DoubleArray

public class DoubleArray extends Object
DoubleArray is a dynamic array of double values. As values are added, the capacity of the array increases.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private double[]
    The array to hold the doubles.
    static final int
    Unless otherwise specified, the array should start with 10 available spaces.
    private int
    The number of valid entries in the array.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructor: create an empty array with DEFAULT_SIZE elements, and set valid to 0.
    DoubleArray(int init_capacity)
    Constructor: create an empty array with init_capacity elements, and set valid to 0.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(double value)
    add the given value to the end of the array.
    double
    Compute the average of all the valid elements in the array.
    int
    Get the current size of the data array.
    boolean
    Determine if this DoubleArray contains the same values as the parameter.
    double
    get(int index)
    Get the value stored at the given index, provided the index is in bounds.
    boolean
    inBounds(int index)
    Check if the given index is at least 0 and less than the number of valid values.
    void
    insertAt(int index, double value)
    Insert the values at the given index, moving all of the original items at that index or higher.
    double
    removeFrom(int index)
    Remove a value from the given index, and return the value which was removed.
    private void
    Called when we need more space.
    void
    set(int index, double value)
    Set the element at the given index to a new value.
    int
    Get the number of valid elements currently being stored.
    double
    sum()
    Compute the sum of all the valid elements in the array.
    Produce a String representation of this object as a comma separated list.

    Methods inherited from class java.lang.Object

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

    • DEFAULT_SIZE

      public static final int DEFAULT_SIZE
      Unless otherwise specified, the array should start with 10 available spaces.
      See Also:
    • data

      private double[] data
      The array to hold the doubles.
    • valid

      private int valid
      The number of valid entries in the array. Valid entries are those that are results of a call to add or insertAt.
  • Constructor Details

    • DoubleArray

      public DoubleArray()
      Constructor: create an empty array with DEFAULT_SIZE elements, and set valid to 0.
    • DoubleArray

      public DoubleArray(int init_capacity)
      Constructor: create an empty array with init_capacity elements, and set valid to 0. The initial capacity must be at least DEFAULT_SIZE.
      Parameters:
      init_capacity - The initial capacity of the array
  • Method Details

    • inBounds

      public boolean inBounds(int index)
      Check if the given index is at least 0 and less than the number of valid values.
      Parameters:
      index - The index to be tested.
      Returns:
      True if the index represents a valid index, false otherwise.
    • get

      public double get(int index)
      Get the value stored at the given index, provided the index is in bounds.
      Parameters:
      index - The index of the value to retrieve.
      Returns:
      The value at the index or NaN (not a number) if the index is out of bounds.
    • set

      public void set(int index, double value)
      Set the element at the given index to a new value. No change will be made if the index is out of bounds, but an error message will be printed.
      Parameters:
      index - The index of the element to set.
      value - The new value.
    • add

      public void add(double value)
      add the given value to the end of the array. Double the capacity of the array if necessary.
      Parameters:
      value - The value to add.
    • capacity

      public int capacity()
      Get the current size of the data array. Note this is different from the size() method.
      Returns:
      Return the length of the underling array.
    • size

      public int size()
      Get the number of valid elements currently being stored. Note this is different from the capacity() method.
      Returns:
      Number of valid elements in the data array.
    • toString

      public String toString()
      Produce a String representation of this object as a comma separated list. For example: { 0.00, 12.34 }
      Overrides:
      toString in class Object
      Returns:
      A String representation of this object.
    • sum

      public double sum()
      Compute the sum of all the valid elements in the array.
      Returns:
      The sum of all of the elements.
    • average

      public double average()
      Compute the average of all the valid elements in the array.
      Returns:
      The average of all of the elements.
    • insertAt

      public void insertAt(int index, double value)
      Insert the values at the given index, moving all of the original items at that index or higher. For example, if the data is {1, 2, 3, 4} and insert is called with index 2 with a value of 1.5 the resulting data will be {1, 2, 1.5, 3, 4}. It is legal to insert on the end of the data. Following the previous example insertAt(4, 5.1) will change the data to be {1, 2, 1.5, 3, 4, 5.1}. Normally, this index is out of bounds.
      Parameters:
      index - The index at which to insert.
      value - The value to be inserted.
    • removeFrom

      public double removeFrom(int index)
      Remove a value from the given index, and return the value which was removed. If the index is out of bounds, don't change anything and return NaN (not a number). Values at locations above index will be shifted down to fill the gap.
      Parameters:
      index - the location of the item to be removed.
      Returns:
      The value that was removed.
    • equals

      public boolean equals(DoubleArray other)
      Determine if this DoubleArray contains the same values as the parameter. For the two DoubleArrays to be equal, they must be the same size and contain the same values at the same locations. They can have different capacities, and still be equal.
      Parameters:
      other - The other DoubleArray to compare this to.
      Returns:
      Return true, if the two data arrays are equal.
    • resize

      private void resize()
      Called when we need more space. Double the size of the data array, and copy the old values into the new array. Finally, update the instance variable, data.