Package dsa.lab03.exercises
Class CircularDynamicArray<Item>
java.lang.Object
dsa.lab03.exercises.CircularDynamicArray<Item>
- Type Parameters:
- Item- the item type
- All Implemented Interfaces:
- Container<Item>,- DynamicSequence<Item>,- StaticSequence<Item>,- Iterable<Item>
A circular dynamic array.
 
A dynamic sequence implemented using an array with spare capacity and variable start index.
Dynamic operations only rarely reallocate a new array.
 Improves on non-circular dynamic arrays' efficiencies with
 
 and invalid reference
#insertFirst(Item)DynamicSequence.removeFirst()
 having (amortised) asymptotic complexity O(1).
- 
Nested Class SummaryNested classes/interfaces inherited from interface dsa.lab02.base.StaticSequenceStaticSequence.ForwardIterator<Item>, StaticSequence.ReverseIterator<Item>
- 
Constructor SummaryConstructorsConstructorDescriptionConstruct an empty circular dynamic array.CircularDynamicArray(Item... items) Construct a circular dynamic array containing the given items.CircularDynamicArray(Iterable<Item> items) Construct a circular dynamic array containing the given items.CircularDynamicArray(Iterable<Item> items, int size) Construct a circular dynamic array containing the given items more efficiently thanCircularDynamicArray(Iterable).
- 
Method SummaryModifier 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.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface dsa.lab02.base.DynamicSequenceinsertFirst, insertLast, removeFirst, removeLastMethods inherited from interface java.lang.IterableforEach, spliterator
- 
Constructor Details- 
CircularDynamicArraypublic CircularDynamicArray()Construct an empty circular dynamic array.
- 
CircularDynamicArrayConstruct a circular dynamic array containing the given items.- Parameters:
- items- the items
 
- 
CircularDynamicArrayConstruct a circular dynamic array containing the given items more efficiently thanCircularDynamicArray(Iterable).- Parameters:
- items- the items
- size- the number of items
- Throws:
- IllegalArgumentException- if- size!=- n(where- nis- items's size)
 
- 
CircularDynamicArrayConstruct a circular dynamic array containing the given items.- Parameters:
- items- the items
 
 
- 
- 
Method Details- 
sizepublic int size()Description copied from interface:ContainerGet the number of contained items.
- 
capacitypublic int capacity()Get the maximum number of items that can be contained without reallocation.- Returns:
- the capacity
 
- 
getDescription copied from interface:StaticSequenceGet the item at the given index.- Specified by:
- getin interface- StaticSequence<Item>
- Parameters:
- index- the index
- Returns:
- the item that is at that index
- Throws:
- IndexOutOfBoundsException- if- index< 0 or- index>=- n(where- nis the size)
 
- 
setDescription copied from interface:StaticSequenceSet the item at the given index.Replaces whatever item was there before. - Specified by:
- setin interface- StaticSequence<Item>
- Parameters:
- index- the index
- item- the new item that should now be at that index
- Throws:
- IndexOutOfBoundsException- if- index< 0 or- index>=- n(where- nis the size)
 
- 
insertDescription 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 interface- DynamicSequence<Item>
- Parameters:
- index- the index
- item- the new item
- Throws:
- IndexOutOfBoundsException- if- index< 0 or- index>- n(where- nis the size)
 
- 
removeDescription 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 interface- DynamicSequence<Item>
- Parameters:
- index- the index
- Returns:
- the item that was at that index
- Throws:
- IndexOutOfBoundsException- if- index< 0 or- index>=- n(where- nis the size)
 
 
-