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 Summary
ConstructorDescriptionDoubleArray
(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 Summary
Modifier and TypeMethodDescriptionvoid
add
(double element) Adds a new element, provided size() is less than length(); and otherwise throws an exception.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.double[]
array()
Exposes a reference to the wrapped array.void
clear()
Logically clears the array.boolean
contains
(double element) Checks if the array contains a value within the first size() elements.boolean
Checks if this DoubleArray is equal to another DoubleArray, such that they wrap the same array instance and are of the same size().double
get
(int index) Gets the element at a specified index.int
hashCode()
Computes a hashCode for the DoubleArray.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.boolean
isEmpty()
Checks if the array is logically empty.int
lastIndexOf
(double element) Gets the index of the last occurrence of an element, provided it appears within the first size() elements.int
length()
Gets the length of the wrapped array.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
size()
Gets the size of the array (i.e., the number of elements added via theadd(double)
methods).void
sort()
Sorts the first size() elements of the array into ascending order.toString()
Generate a String representation of the DoubleArray.
-
Constructor Details
-
DoubleArray
public 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
-
DoubleArray
public 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
-
add
public 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()
-
add
public 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()
-
array
public double[] array()Exposes a reference to the wrapped array.- Returns:
- a reference to the wrapped array
-
clear
public void clear()Logically clears the array. That is, sets size() to 0, but the contents of the array is otherise untouched. -
contains
public 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.
-
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, 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.
-
isEmpty
public boolean isEmpty()Checks if the array is logically 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, 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.
-
length
public int length()Gets the length of the wrapped array.- Returns:
- the length of the wrapped array
-
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 logically 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 logically 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 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.
-
sort
public void sort()Sorts the first size() elements of the array into ascending order. -
equals
Checks if this DoubleArray is equal to another DoubleArray, such that they wrap the same array instance and are of the same size(). -
hashCode
public int hashCode()Computes a hashCode for the DoubleArray. -
toString
Generate a String representation of the DoubleArray.
-