Class DoubleArray
java.lang.Object
DoubleArray
DoubleArray is a dynamic array of double values. As values are added, the capacity
of the array increases.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate double[]The array to hold the doubles.static final intUnless otherwise specified, the array should start with 10 available spaces.private intThe number of valid entries in the array. -
Constructor Summary
ConstructorsConstructorDescriptionConstructor: 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 TypeMethodDescriptionvoidadd(double value) add the given value to the end of the array.doubleaverage()Compute the average of all the valid elements in the array.intcapacity()Get the current size of the data array.booleanequals(DoubleArray other) Determine if this DoubleArray contains the same values as the parameter.doubleget(int index) Get the value stored at the given index, provided the index is in bounds.booleaninBounds(int index) Check if the given index is at least 0 and less than the number of valid values.voidinsertAt(int index, double value) Insert the values at the given index, moving all of the original items at that index or higher.doubleremoveFrom(int index) Remove a value from the given index, and return the value which was removed.private voidresize()Called when we need more space.voidset(int index, double value) Set the element at the given index to a new value.intsize()Get the number of valid elements currently being stored.doublesum()Compute the sum of all the valid elements in the array.toString()Produce a String representation of this object as a comma separated list.
-
Field Details
-
DEFAULT_SIZE
public static final int DEFAULT_SIZEUnless otherwise specified, the array should start with 10 available spaces.- See Also:
-
data
private double[] dataThe array to hold the doubles. -
valid
private int validThe 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
Produce a String representation of this object as a comma separated list. For example: { 0.00, 12.34 } -
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
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.
-