Package cs2110
Class ArraySeq<T>
java.lang.Object
cs2110.ArraySeq<T>
A sequence of non-null elements of type `T` implemented using a dynamically resized array. The
array begins with some initial capacity and doubles in size whenever adding an element would
cause the sequence to exceed its capacity.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate classA private inner class that acts as an iterator over an ArraySeq. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate T[]Backing storage of this sequence.private static final intThe initial capacity of the backing storage for new instances of `ArraySeq`.private intNumber of elements in the sequence. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidAdd element `elem` to the end of this list.booleanReturn whether this list contains an element equal to `elem` (according to `equals()`).booleanReturn whether this and `other` are `ArraySeq`s containing the same elements in the same order.private intfirstIndexOf(T elem) Return the smallest index i such that `contents[i].equals(elem)`, or -1 if this sequence does not contain `elem`.get(int index) Return the element at index `index` in this list.inthashCode()Returns a hash code value for the object.voidinsertBefore(T elem, T successor) Insert element `elem` into the list just before the first occurrence of element `successor`.iterator()Return an iterator over the elements of this list (in sequence order).voidInsert element `elem` at the beginning of this list.booleanRemove the first occurrence of element `elem` (if any) from this list.private voidIf our backing storage is full, replace it with an array that is twice as large while preserving its contents.intsize()Return the number of elements in this list.toString()Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Field Details
-
INITIAL_CAPACITY
private static final int INITIAL_CAPACITYThe initial capacity of the backing storage for new instances of `ArraySeq`.- See Also:
-
contents
Backing storage of this sequence. Indices 0..size-1 are non-null while indices size..contents.length are null. Length must be at least 1. -
size
private int sizeNumber of elements in the sequence. Must be non-negative and at most `contents.length`
-
-
Constructor Details
-
ArraySeq
public ArraySeq()Create an empty list.
-
-
Method Details
-
size
public int size()Description copied from interface:SeqReturn the number of elements in this list. -
get
Description copied from interface:SeqReturn the element at index `index` in this list. The index of the first element is 0. Requires0 <= index < size(). Will not return null. -
resizeIfNeeded
private void resizeIfNeeded()If our backing storage is full, replace it with an array that is twice as large while preserving its contents. Otherwise, do nothing. -
prepend
Description copied from interface:SeqInsert element `elem` at the beginning of this list. Example: if the list is [8, 7, 4], prepend(2) would change the list to [2, 8, 7, 4]. Requires `elem` is not null. -
append
Description copied from interface:SeqAdd element `elem` to the end of this list. Example: if the list is [8, 7, 4], append(2) would change the list to [8, 7, 4, 2]. Requires `elem` is not null. -
firstIndexOf
Return the smallest index i such that `contents[i].equals(elem)`, or -1 if this sequence does not contain `elem`. -
contains
Description copied from interface:SeqReturn whether this list contains an element equal to `elem` (according to `equals()`). Requires `elem` is not null. -
insertBefore
Description copied from interface:SeqInsert element `elem` into the list just before the first occurrence of element `successor`. Requires that `successor` is contained in the list and that `elem` and `successor` are not null.Example: If the list is [3, 8, 2], then insertBefore(1, 8) would change the list to [3, 1, 8, 2].
- Specified by:
insertBeforein interfaceSeq<T>
-
remove
Description copied from interface:SeqRemove the first occurrence of element `elem` (if any) from this list. Return whether the list changed. Requires `elem` is not null. -
equals
Return whether this and `other` are `ArraySeq`s containing the same elements in the same order. Two elements `e1` and `e2` are "the same" if `e1.equals(e2)`. Note that `ArraySeq` is mutable, so equivalence between two objects may change over time. See `Object.equals()` for additional guarantees. -
hashCode
public int hashCode()Returns a hash code value for the object. See `Object.hashCode()` for additional guarantees. -
toString
-
iterator
Return an iterator over the elements of this list (in sequence order). By implementing `Iterable`, clients can use Java's "enhanced for-loops" to iterate over the elements of the list.
-