Package dsa.lab03.exercises
Class DynamicArray<Item>
java.lang.Object
dsa.lab03.exercises.DynamicArray<Item>
- Type Parameters:
Item- the item type
- All Implemented Interfaces:
Container<Item>,DynamicSequence<Item>,StaticSequence<Item>,Iterable<Item>
A dynamic array.
A dynamic sequence implemented using an array with spare capacity.
Dynamic operations only rarely reallocate a new array.
Improves on static arrays' efficiencies with
and invalid reference
#insertLast(Item)DynamicSequence.removeLast()
having (amortised) asymptotic complexity O(1).
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface dsa.lab02.base.StaticSequence
StaticSequence.ForwardIterator<Item>, StaticSequence.ReverseIterator<Item> -
Constructor Summary
ConstructorsConstructorDescriptionConstruct an empty dynamic array.DynamicArray(Item... items) Construct a dynamic array containing the given items.DynamicArray(Iterable<Item> items) Construct a dynamic array containing the given items.DynamicArray(Iterable<Item> items, int size) Construct a dynamic array containing the given items more efficiently thanDynamicArray(Iterable). -
Method Summary
Modifier and TypeMethodDescriptionintcapacity()Get the maximum number of items that can be contained without reallocation.get(int index) Get the item at the given index.voidInsert the given item at the given index.remove(int index) Remove and return the item at the given index.voidSet the item at the given index.intsize()Get the number of contained items.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface dsa.lab02.base.DynamicSequence
insertFirst, insertLast, removeFirst, removeLastMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
DynamicArray
public DynamicArray()Construct an empty dynamic array. -
DynamicArray
Construct a dynamic array containing the given items.- Parameters:
items- the items
-
DynamicArray
Construct a dynamic array containing the given items more efficiently thanDynamicArray(Iterable).- Parameters:
items- the itemssize- the number of items- Throws:
IllegalArgumentException- ifsize!=n(wherenisitems's size)
-
DynamicArray
Construct a dynamic array containing the given items.- Parameters:
items- the items
-
-
Method Details
-
size
public int size()Description copied from interface:ContainerGet the number of contained items. -
capacity
public int capacity()Get the maximum number of items that can be contained without reallocation.- Returns:
- the capacity
-
get
Description copied from interface:StaticSequenceGet the item at the given index.- Specified by:
getin interfaceStaticSequence<Item>- Parameters:
index- the index- Returns:
- the item that is at that index
- Throws:
IndexOutOfBoundsException- ifindex< 0 orindex>=n(wherenis the size)
-
set
Description copied from interface:StaticSequenceSet the item at the given index.Replaces whatever item was there before.
- Specified by:
setin interfaceStaticSequence<Item>- Parameters:
index- the indexitem- the new item that should now be at that index- Throws:
IndexOutOfBoundsException- ifindex< 0 orindex>=n(wherenis the size)
-
insert
Description copied from interface:DynamicSequenceInsert the given item at the given index.Increments the size, as well as the indices of all items that had the same index or higher.
- Specified by:
insertin interfaceDynamicSequence<Item>- Parameters:
index- the indexitem- the new item- Throws:
IndexOutOfBoundsException- ifindex< 0 orindex>n(wherenis the size)
-
remove
Description copied from interface:DynamicSequenceRemove and return the item at the given index.Decrements the size, as well as the indices of all items that had the same index or higher.
- Specified by:
removein interfaceDynamicSequence<Item>- Parameters:
index- the index- Returns:
- the item that was at that index
- Throws:
IndexOutOfBoundsException- ifindex< 0 orindex>=n(wherenis the size)
-