java.lang.Object
org.cicirello.util.DoubleArray
An object of this class wraps an array of primitive doubles in such a way as to provide some
 ArrayList-like operations for a primitive array (e.g., using it as a partially-filled array). It
 doesn't support growable operations, instead keeping array length fixed. An instance of this
 class does not strongly encapsulate the array. If initialized from an existing array, it
 maintains a reference to that array, and regardless also provides an 
array method that
 exposes a reference to the array.
 For a strongly encapsulated partially-filled array of primitive doubles, including
 growability, see the DoubleList class.
- 
Constructor SummaryConstructorsConstructorDescriptionDoubleArray(double[] array) Initializes an DoubleArray so as to wrap an existing array.DoubleArray(int length) Initializes an empty DoubleArray, with a specified length for the internal array.
- 
Method SummaryModifier and TypeMethodDescriptionvoidadd(double element) Adds a new element, provided size() is less than length(); and otherwise throws an exception.voidadd(int index, double element) Inserts a new element at a specified index, provided size() is less than length(); and otherwise throws an exception.double[]array()Exposes a reference to the wrapped array.voidclear()Logically clears the array.booleancontains(double element) Checks if the array contains a value within the first size() elements.booleanChecks if this DoubleArray is equal to another DoubleArray, such that they wrap the same array instance and are of the same size().doubleget(int index) Gets the element at a specified index.inthashCode()Computes a hashCode for the DoubleArray.intindexOf(double element) Gets the index of the first occurrence of an element from the left, provided it appears within the first size() elements.booleanisEmpty()Checks if the array is logically empty.intlastIndexOf(double element) Gets the index of the last occurrence of an element, provided it appears within the first size() elements.intlength()Gets the length of the wrapped array.doubleremove(int index) Removes the element at a specified index.voidset(int index, double element) Sets an element at a specified index, replacing any value that is currently there.intsize()Gets the size of the array (i.e., the number of elements added via theadd(double)methods).voidsort()Sorts the first size() elements of the array into ascending order.toString()Generate a String representation of the DoubleArray.
- 
Constructor Details- 
DoubleArraypublic DoubleArray(int length) Initializes an empty DoubleArray, with a specified length for the internal array. By empty, we mean size() equals 0.- Parameters:
- length- the length of the array within the DoubleArray
- Throws:
- NegativeArraySizeException- if length is negative
 
- 
DoubleArraypublic DoubleArray(double[] array) Initializes an DoubleArray so as to wrap an existing array. A reference to this array is stored in the DoubleArray object, and thus changes to the array externally are observable within the DoubleArray; and likewise changes via the methods of this class are observable directly via the array reference.The size() of this DoubleArray is 0, and doesn't depend upon the length of array. However, the length() of this DoubleArray equals array.length. - Parameters:
- array- the array to wrap
 
 
- 
- 
Method Details- 
addpublic void add(double element) Adds a new element, provided size() is less than length(); and otherwise throws an exception.- Parameters:
- element- The value to add.
- Throws:
- IndexOutOfBoundsException- if size() is equal to length()
 
- 
addpublic void add(int index, double element) Inserts a new element at a specified index, provided size() is less than length(); and otherwise throws an exception.- Parameters:
- index- The index for the insertion.
- element- The value to add.
- Throws:
- IndexOutOfBoundsException- if size() is equal to length() or if index is greater than size()
 
- 
arraypublic double[] array()Exposes a reference to the wrapped array.- Returns:
- a reference to the wrapped array
 
- 
clearpublic void clear()Logically clears the array. That is, sets size() to 0, but the contents of the array is otherise untouched.
- 
containspublic boolean contains(double element) Checks if the array contains a value within the first size() elements.- Parameters:
- element- The element to search for.
- Returns:
- true if and only if the array contains at least one copy of element within the first size() elements.
 
- 
getpublic 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().
 
- 
indexOfpublic int indexOf(double element) Gets the index of the first occurrence of an element from the left, provided it appears within the first size() elements.- Parameters:
- element- The element searched for.
- Returns:
- The index of the first occurrence of the element in the array from the left, or -1 if the element doesn't exist within the first size() elements.
 
- 
isEmptypublic boolean isEmpty()Checks if the array is logically empty.- Returns:
- true if and only if size() equals 0.
 
- 
lastIndexOfpublic int lastIndexOf(double element) Gets the index of the last occurrence of an element, provided it appears within the first size() elements.- Parameters:
- element- The element searched for.
- Returns:
- The index of the last occurrence of the element in the array within the first size() elements, or -1 if the element doesn't exist.
 
- 
lengthpublic int length()Gets the length of the wrapped array.- Returns:
- the length of the wrapped array
 
- 
removepublic 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 logically out of bounds, such that index is negative or index is greater than or equal to size().
 
- 
setpublic 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 logically out of bounds, such that index is negative or index is greater than or equal to size().
 
- 
sizepublic int size()Gets the size of the array (i.e., the number of elements added via theadd(double)methods).- Returns:
- The size of the array, which is the number of elements currently within it.
 
- 
sortpublic void sort()Sorts the first size() elements of the array into ascending order.
- 
equalsChecks if this DoubleArray is equal to another DoubleArray, such that they wrap the same array instance and are of the same size().
- 
hashCodepublic int hashCode()Computes a hashCode for the DoubleArray.
- 
toStringGenerate a String representation of the DoubleArray.
 
-