Package com.zybooks.dsaj.list
Interface PositionalList<E>
- Type Parameters:
E
- the element type stored at a position
- All Superinterfaces:
Iterable<E>
- All Known Implementing Classes:
LinkedPositionalList
An interface for positional lists.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionInserts an element immediately after the given Position.Inserts an element immediately before the given Position.Inserts an element at the front of the list.Inserts an element at the back of the list.Returns the Position immediately after Position p.Returns the Position immediately before Position p.first()
Returns the first Position in the list.boolean
isEmpty()
Tests whether the list is empty.iterator()
Returns an iterator of the elements stored in the list.last()
Returns the last Position in the list.Returns the positions of the list in iterable form from first to last.Removes the element stored at the given Position and returns it.Replaces the element stored at the given Position and returns the replaced element.int
size()
Returns the number of elements in the list.Methods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
size
int size()Returns the number of elements in the list.- Returns:
- number of elements in the list
-
isEmpty
boolean isEmpty()Tests whether the list is empty.- Returns:
- true if the list is empty, false otherwise
-
first
Returns the first Position in the list.- Returns:
- the first Position in the list (or null, if empty)
-
last
Returns the last Position in the list.- Returns:
- the last Position in the list (or null, if empty)
-
before
Returns the Position immediately before Position p.- Parameters:
p
- a Position of the list- Returns:
- the Position of the preceding element (or null, if p is first)
- Throws:
IllegalArgumentException
- if p is not a valid position for this list
-
after
Returns the Position immediately after Position p.- Parameters:
p
- a Position of the list- Returns:
- the Position of the following element (or null, if p is last)
- Throws:
IllegalArgumentException
- if p is not a valid position for this list
-
addFirst
Inserts an element at the front of the list.- Parameters:
e
- the new element- Returns:
- the Position representing the location of the new element
-
addLast
Inserts an element at the back of the list.- Parameters:
e
- the new element- Returns:
- the Position representing the location of the new element
-
addBefore
Inserts an element immediately before the given Position.- Parameters:
p
- the Position before which the insertion takes placee
- the new element- Returns:
- the Position representing the location of the new element
- Throws:
IllegalArgumentException
- if p is not a valid position for this list
-
addAfter
Inserts an element immediately after the given Position.- Parameters:
p
- the Position after which the insertion takes placee
- the new element- Returns:
- the Position representing the location of the new element
- Throws:
IllegalArgumentException
- if p is not a valid position for this list
-
set
Replaces the element stored at the given Position and returns the replaced element.- Parameters:
p
- the Position of the element to be replacede
- the new element- Returns:
- the replaced element
- Throws:
IllegalArgumentException
- if p is not a valid position for this list
-
remove
Removes the element stored at the given Position and returns it. The given position is invalidated as a result.- Parameters:
p
- the Position of the element to be removed- Returns:
- the removed element
- Throws:
IllegalArgumentException
- if p is not a valid position for this list
-
iterator
Returns an iterator of the elements stored in the list. -
positions
Returns the positions of the list in iterable form from first to last.- Returns:
- iterable collection of the list's positions
-