Package dsa.lab02.exercises
Class StaticArray<Item>
java.lang.Object
dsa.lab02.exercises.StaticArray<Item>
- Type Parameters:
Item
- the item type
- All Implemented Interfaces:
Container<Item>
,DynamicSequence<Item>
,StaticSequence<Item>
,Iterable<Item>
A static array.
A dynamic sequence implemented using a full array (i.e. with as many items as slots).
Dynamic operations always reallocate a new array and are all O(n
)
(where n
is the size).
-
Nested Class Summary
Nested classes/interfaces inherited from interface dsa.lab02.base.StaticSequence
StaticSequence.ForwardIterator<Item>, StaticSequence.ReverseIterator<Item>
-
Constructor Summary
ConstructorsConstructorDescriptionConstruct an empty static array.StaticArray
(Item... items) Construct a static array containing the given items.StaticArray
(Iterable<Item> items) Construct a static array containing the given items.StaticArray
(Iterable<Item> items, int size) Construct a static array containing the given items more efficiently thanStaticArray(Iterable)
. -
Method Summary
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface dsa.lab02.base.DynamicSequence
insertFirst, insertLast, removeFirst, removeLast
Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
StaticArray
public StaticArray()Construct an empty static array. -
StaticArray
Construct a static array containing the given items.- Parameters:
items
- the items
-
StaticArray
Construct a static array containing the given items more efficiently thanStaticArray(Iterable)
.- Parameters:
items
- the itemssize
- the number of items- Throws:
IllegalArgumentException
- ifsize
!=n
(wheren
isitems
's size)
-
StaticArray
Construct a static array containing the given items.- Parameters:
items
- the items
-
-
Method Details
-
size
public int size()Description copied from interface:Container
Get the number of contained items. -
get
Description copied from interface:StaticSequence
Get the item at the given index.- Specified by:
get
in interfaceStaticSequence<Item>
- Parameters:
index
- the index- Returns:
- the item that is at that index
- Throws:
IndexOutOfBoundsException
- ifindex
< 0 orindex
>=n
(wheren
is the size)
-
set
Description copied from interface:StaticSequence
Set the item at the given index.Replaces whatever item was there before.
- Specified by:
set
in interfaceStaticSequence<Item>
- Parameters:
index
- the indexitem
- the new item that should now be at that index- Throws:
IndexOutOfBoundsException
- ifindex
< 0 orindex
>=n
(wheren
is the size)
-
insert
Description copied from interface:DynamicSequence
Insert 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:
insert
in interfaceDynamicSequence<Item>
- Parameters:
index
- the indexitem
- the new item- Throws:
IndexOutOfBoundsException
- ifindex
< 0 orindex
>n
(wheren
is the size)
-
remove
Description copied from interface:DynamicSequence
Remove 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:
remove
in interfaceDynamicSequence<Item>
- Parameters:
index
- the index- Returns:
- the item that was at that index
- Throws:
IndexOutOfBoundsException
- ifindex
< 0 orindex
>=n
(wheren
is the size)
-