Class DoubleArraySeq - Millersville Variation
java.lang.Object
extended by edu.colorado.collections.DoubleArraySeq
- All Implemented Interfaces:
- java.lang.Cloneable
public class DoubleArraySeq
- extends java.lang.Object
- implements java.lang.Cloneable
This class is a homework assignment.
A DoubleArraySeq is a collection of double numbers.
The sequence can have a special "current element," which is specified and
accessed through four methods that are not available in the bag class
(start, getCurrent, advance and isCurrent).
- See Also:
- Unmodified older Java Source Code developed by Michael Main for this class can be found at
(www.cs.colorado.edu/~main/edu/colorado/collections/DoubleArraySeq.java)
- Note:
- (1) The capacity of a sequence can change after it's created, but
the maximum capacity is limited by the amount of free memory on the
machine. The constructor, addAfter,
addBefore, clone,
and concatenation will result in an
OutOfMemoryError when free memory is exhausted.
(2) A sequence's capacity cannot exceed the maximum integer 2,147,483,647
(Integer.MAX_VALUE). Any attempt to create a larger capacity
results in a failure due to an arithmetic overflow.
Constructor Summary |
DoubleArraySeq()
Initialize an empty sequence with an initial capacity of 10. |
DoubleArraySeq(int initialCapacity)
Initialize an empty sequence with a specified initial capacity. |
Method Summary |
void |
addAfter(double element)
Add a new element to this sequence, after the current element. |
void |
addAll(DoubleArraySeq addend)
Place the contents of another sequence at the end of this sequence. |
void |
addBefore(double element)
Add a new element to this sequence, before the current element. |
void |
advance( )
Move forward, so that the current element is now the next element in
this sequence. |
static DoubleArraySeq |
concatenation(DoubleArraySeq s1,
DoubleArraySeq s2)
Create a new sequence that contains all the elements from one sequence followed by all the elements from another sequence. |
DoubleArraySeq |
clone( )
Generate a separate copy of this sequence. |
void |
ensureCapacity(int minimumCapacity)
Change the current capacity of this sequence. |
int |
getCapacity( )
Accessor method to get the current capacity of this sequence. |
double |
getCurrent( )
Accessor method to get the current element of this sequence. |
boolean |
isCurrent( )
Accessor method to determine whether this sequence has a specified
current element that can be retrieved with the
getCurrent method. |
void |
removeCurrent()
Remove the current element from this sequence. |
int |
size( )
Determine the number of elements in this sequence. |
void |
start( )
Set the current element at the front of this sequence. |
String |
toString( )
Provide a string representation of the sequence with current item in parentheses. |
void |
trimToSize()
Reduce the current capacity of this sequence to its actual size (i.e., the
number of elements it contains). |
Methods inherited from class java.lang.Object |
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
DoubleArraySeq
public DoubleArraySeq()
- Initialize an empty sequence with an initial capacity of 10. Note that
the addAfter and addBefore methods work
efficiently (without needing more memory) until this capacity is reached.
- Parameters:
-
- none- Postcondition:
- This sequence is empty and has an initial capacity of 10.
- Throws:
java.lang.OutOfMemoryError
- Indicates insufficient memory for:
new double[10].
DoubleArraySeq
public DoubleArraySeq(int initialCapacity)
- Initialize an empty sequence with a specified initial capacity. Note that
the addAfter and addBefore methods work
efficiently (without needing more memory) until this capacity is reached.
- Parameters:
initialCapacity
- the initial capacity of this sequence- Precondition:
- initialCapacity is non-negative.
- Postcondition:
- This sequence is empty and has the given initial capacity.
- Throws:
java.lang.IllegalArgumentException
- Indicates that initialCapacity is negative.
java.lang.OutOfMemoryError
- Indicates insufficient memory for:
new double[initialCapacity].
addAfter
public void addAfter(double element)
- Add a new element to this sequence, after the current element.
If the new element would take this sequence beyond its current capacity,
then the capacity is increased before adding the new element.
- Parameters:
element
- the new element that is being added- Postcondition:
- A new copy of the element has been added to this sequence. If there was
a current element, then the new element is placed after the current
element. If there was no current element, then the new element is placed
at the end of the sequence. In all cases, the new element becomes the
new current element of this sequence.
- Throws:
java.lang.OutOfMemoryError
- Indicates insufficient memory for increasing the sequence's capacity.- Note:
- An attempt to increase the capacity beyond
Integer.MAX_VALUE will cause the sequence to fail with an
arithmetic overflow.
addBefore
public void addBefore(double element)
- Add a new element to this sequence, before the current element.
If the new element would take this sequence beyond its current capacity,
then the capacity is increased before adding the new element.
- Parameters:
element
- the new element that is being added- Postcondition:
- A new copy of the element has been added to this sequence. If there was
a current element, then the new element is placed before the current
element. If there was no current element, then the new element is placed
at the start of the sequence. In all cases, the new element becomes the
new current element of this sequence.
- Throws:
java.lang.OutOfMemoryError
- Indicates insufficient memory for increasing the sequence's capacity.- Note:
- An attempt to increase the capacity beyond
Integer.MAX_VALUE will cause the sequence to fail with an
arithmetic overflow.
addAll
public void addAll(DoubleArraySeq addend)
- Place the contents of another sequence at the end of this sequence.
- Parameters:
addend
- a sequence whose contents will be placed at the end of this sequence- Precondition:
- The parameter, addend, is not null.
- Postcondition:
- The elements from addend have been placed at the end of
this sequence. The current element of this sequence remains where it
was, and the addend is also unchanged.
- Throws:
java.lang.NullPointerException
- Indicates that addend is null.
java.lang.OutOfMemoryError
- Indicates insufficient memory to increase the size of this sequence.- Note:
- An attempt to increase the capacity beyond
Integer.MAX_VALUE will cause an arithmetic overflow
that will cause the sequence to fail.
advance
public void advance()
- Move forward, so that the current element is now the next element in
this sequence.
- Parameters:
-
- none- Precondition:
- isCurrent() returns true.
- Postcondition:
- If the current element was already the end element of this sequence
(with nothing after it), then there is no longer any current element.
Otherwise, the new element is the element immediately after the
original current element.
- Throws:
java.lang.IllegalStateException
- Indicates that there is no current element, so
advance may not be called.
clone
public DoubleArraySeq clone()
- Generate a separate copy of this sequence.
- Overrides:
clone
in class java.lang.Object
- Parameters:
-
- none
- Returns:
- The return value is a copy of this sequence. Subsequent changes to the
copy will not affect the original, nor vice versa.
- Throws:
java.lang.OutOfMemoryError
- Indicates insufficient memory for creating the clone.
concatenation
public static DoubleArraySeq concatenation(DoubleArraySeq s1,
DoubleArraySeq s2)
- Create a new sequence that contains all the elements from one sequence
followed by another.
- Parameters:
s1
- the first of two sequencess2
- the second of two sequences- Precondition:
- Neither s1 nor s2 is null.
- Returns:
- a new sequence that has the elements of s1 followed by the
elements of s2 (with no current element)
- Throws:
NullPointerException.
- Indicates that one of the arguments is null.
java.lang.OutOfMemoryError
- Indicates insufficient memory for the new sequence.- Note:
- An attempt to create a sequence with a capacity beyond
Integer.MAX_VALUE will cause an arithmetic overflow
that will cause the sequence to fail.
ensureCapacity
public void ensureCapacity(int minimumCapacity)
- Change the current capacity of this sequence.
- Parameters:
minimumCapacity
- the new capacity for this sequence- Postcondition:
- This sequence's capacity has been changed to at least minimumCapacity.
If the capacity was already at or greater than minimumCapacity,
then the capacity is left unchanged.
- Throws:
java.lang.OutOfMemoryError
- Indicates insufficient memory for: new int[minimumCapacity].
getCapacity
public int getCapacity()
- Accessor method to get the current capacity of this sequence.
The add method works efficiently (without needing
more memory) until this capacity is reached.
- Parameters:
-
- none
- Returns:
- the current capacity of this sequence
getCurrent
public double getCurrent()
- Accessor method to get the current element of this sequence.
- Parameters:
-
- none- Precondition:
- isCurrent() returns true.
- Returns:
- the current element of this sequence
- Throws:
java.lang.IllegalStateException
- Indicates that there is no current element, so
getCurrent may not be called.
isCurrent
public boolean isCurrent()
- Accessor method to determine whether this sequence has a specified
current element that can be retrieved with the
getCurrent method.
- Parameters:
-
- none
- Returns:
- true (there is a current element) or false (there is no current element at the moment)
removeCurrent
public void removeCurrent()
- Remove the current element from this sequence.
- Parameters:
-
- none- Precondition:
- isCurrent() returns true.
- Postcondition:
- The current element has been removed from this sequence, and the
following element (if there is one) is now the new current element.
If there was no following element, then there is now no current
element.
- Throws:
java.lang.IllegalStateException
- Indicates that there is no current element, so
removeCurrent may not be called.
size
public int size()
- Determine the number of elements in this sequence.
- Parameters:
-
- none
- Returns:
- the number of elements in this sequence
start
public void start()
- Set the current element at the front of this sequence.
- Parameters:
-
- none- Postcondition:
- The front element of this sequence is now the current element (but
if this sequence has no elements at all, then there is no current
element).
toString
public java.lang.String toString()
- Provide a string representation of the sequence with current
item in parentheses
- Overrides:
toString
in class java.lang.Object
- Parameters:
-
- none
- Returns:
- string displaying sequence
trimToSize
public void trimToSize()
- Reduce the current capacity of this sequence to its actual size (i.e., the
number of elements it contains). The current item does not change.
- Parameters:
-
- none- Postcondition:
- This sequence's capacity has been changed to its current size.
- Throws:
java.lang.OutOfMemoryError
- Indicates insufficient memory for altering the capacity.