Package utilities
Interface ListADT<E>
- Type Parameters:
E
- The type of elements this list holds.
- All Superinterfaces:
Serializable
- All Known Implementing Classes:
MyArrayList
The ListADT
interface is designed to be used as a basis for all
the Linear data structures that will be developed in the CPRG 304 class at
SAIT. The implementors of this interface will be required to add all the
functionality.
-
Method Summary
Modifier and TypeMethodDescriptionboolean
Inserts the specified element at the specified position in this list.boolean
Appends the specified element to the end of this list.boolean
Appends all the elements in the specifiedjava.utilCollection
to the end of this list, in the order that they are returned by the specified collection'sIterator
.void
clear()
Removes all of the elements from this list.boolean
Returns true if this list contains the specified element.get
(int index) Returns the element at the specified position in this list.boolean
isEmpty()
Returnstrue
if this list contains no elements.iterator()
Returns an iterator over the elements in this list, in a proper sequence.remove
(int index) Removes the element at the specified position in this list.Removes the first occurrence in this list of the specified element.Replaces the element at the specified position in this list with the specified element.int
size()
The size method will return the current element count contained in the list.Object[]
toArray()
Returns an array containing all the elements in this list in a proper sequence.E[]
Returns an array containing all of the elements in this list in a proper sequence; the runtime type of the returned array is that of the specified array.
-
Method Details
-
size
int size()The size method will return the current element count contained in the list.- Returns:
- The current element count.
-
clear
void clear()Removes all of the elements from this list. This list will be empty after this call returns. -
add
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).- Parameters:
index
- The index at which the specified element is to be inserted. The element is inserted before the existing element at [index], or at the end if index is equal to the size (size()
).toAdd
- The element to be inserted.- Returns:
true
if the element is added successfully.- Throws:
NullPointerException
- If the specified element isnull
and the list implementation does not support havingnull
elements.IndexOutOfBoundsException
- If the index is out of range:
i.e. (index < 0 || index >= size()
).
-
add
Appends the specified element to the end of this list. Implementations that support this operation may place limitations on what elements may be added to this list. In particular, some implementations will refuse to addnull
elements. List classes should clearly specify in their documentation any restrictions on what elements may be added.- Parameters:
toAdd
- Element to be appended to this list.- Returns:
- true if the element appended successfully.
- Throws:
NullPointerException
- If the specified element isnull
and the list implementation does not support havingnull
elements.IndexOutOfBoundsException
- If the index is out of range:
i.e. (index < 0 || index >= size()
).
-
addAll
Appends all the elements in the specifiedjava.utilCollection
to the end of this list, in the order that they are returned by the specified collection'sIterator
. The behaviour of this operation is unspecified if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.)- Parameters:
toAdd
- The new sub list to be added.- Returns:
- true If the operation is successful.
- Throws:
NullPointerException
- If the specified element isnull
and the list implementation does not support havingnull
elements.
-
get
Returns the element at the specified position in this list.- Parameters:
index
- Index of the element to return.- Returns:
- The element at the specified position in this list.
- Throws:
IndexOutOfBoundsException
- If the index is out of range:
i.e. (index < 0 || index >= size()
).
-
remove
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). Returns the element that was removed from the list.- Parameters:
index
- The index of the element to remove.- Returns:
- The removed element.
- Throws:
IndexOutOfBoundsException
- If the index is out of range:
i.e. (index < 0 || index >= size()
).
-
remove
Removes the first occurrence in this list of the specified element. If this list does not contain the element, it is unchanged. More formally, removes the element with the lowest indexi
such thato.equals(get(i))
(if such an element exists).- Parameters:
toRemove
- The element to be removed from this list.- Returns:
- The element which is being removed, or null if the list does not contain the element.
- Throws:
NullPointerException
- If the specified element isnull
and the list implementation does not support havingnull
elements.
-
set
Replaces the element at the specified position in this list with the specified element.- Parameters:
index
- The index of the element to replace.toChange
- Element to be stored at the specified position.- Returns:
- The element previously at the specified position.
- Throws:
NullPointerException
- If the specified element isnull
and the list implementation does not support havingnull
elements.IndexOutOfBoundsException
- If the index is out of range:
i.e. (index < 0 || index >= size()
).
-
isEmpty
boolean isEmpty()Returnstrue
if this list contains no elements.- Returns:
true
if this list contains no elements.
-
contains
Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one elemente
such thattoFind.equals(e)
.- Parameters:
toFind
- The element whose presence in this list is to be tested.- Returns:
true
if this list contains the specified element.- Throws:
NullPointerException
- If the specified element isnull
and the list implementation does not support havingnull
elements.
-
toArray
Returns an array containing all of the elements in this list in a proper sequence; the runtime type of the returned array is that of the specified array. Obeys the general contract of thejava.util.Collection.toArray(Object [])
method.- Parameters:
toHold
- The array into which the elements of this list are to be stored if it is big enough; otherwise, a new array of the same runtime type is allocated for this purpose.- Returns:
- An array containing the elements of this list.
- Throws:
NullPointerException
- If the specified array isnull
.
-
toArray
Object[] toArray()Returns an array containing all the elements in this list in a proper sequence. Obeys the general contract of thejava.util.Collection.toArray()
method.- Returns:
- An array containing all the elements in this list in a proper sequence.
-
iterator
Returns an iterator over the elements in this list, in a proper sequence.- Returns:
- An iterator over the elements in this list, in a proper sequence.
NB: The return is of type
linearUtilities.Iterator<E>
, notjava.util.Iterator
.
-