Package utilities
Class MyArrayList<E>
java.lang.Object
utilities.MyArrayList<E>
- Type Parameters:
E
- The type of elements in this list.
- All Implemented Interfaces:
Serializable
,ListADT<E>
,StackADT<E>
A custom List implementation.
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionprivate static class
A custom Iterator implementation forMyArrayList
. -
Field Summary
-
Constructor Summary
ConstructorDescriptionInitializes a new instance ofMyArrayList
.MyArrayList
(int capacity) Initializes a new instance ofMyArrayList
. -
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.private void
grow()
A helper method that doubles the list size.boolean
isEmpty()
Returnstrue
if this list contains no elements.iterator()
Returns an iterator over the elements in this list, in a proper sequence.peek()
Returns the data of the element to the last positionpop()
Removes the element in the last positionvoid
Returns the data of the element to the last positionremove
(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.
-
Field Details
-
DEFAULT_VALUE
private static final int DEFAULT_VALUE- See Also:
-
elements
-
size
private int size
-
-
Constructor Details
-
MyArrayList
public MyArrayList()Initializes a new instance ofMyArrayList
. -
MyArrayList
public MyArrayList(int capacity) Initializes a new instance ofMyArrayList
.- Parameters:
capacity
- The initial capacity for this list.
-
-
Method Details
-
grow
private void grow()A helper method that doubles the list size. -
size
public int size()Description copied from interface:ListADT
The size method will return the current element count contained in the list. -
clear
public void clear()Description copied from interface:ListADT
Removes all of the elements from this list. This list will be empty after this call returns. -
add
Description copied from interface:ListADT
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).- Specified by:
add
in interfaceListADT<E>
- 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
Description copied from interface:ListADT
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.- Specified by:
add
in interfaceListADT<E>
- 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
Description copied from interface:ListADT
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.)- Specified by:
addAll
in interfaceListADT<E>
- 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
Description copied from interface:ListADT
Returns the element at the specified position in this list.- Specified by:
get
in interfaceListADT<E>
- 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
Description copied from interface:ListADT
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.- Specified by:
remove
in interfaceListADT<E>
- 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
Description copied from interface:ListADT
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).- Specified by:
remove
in interfaceListADT<E>
- 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
Description copied from interface:ListADT
Replaces the element at the specified position in this list with the specified element.- Specified by:
set
in interfaceListADT<E>
- 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
public boolean isEmpty()Description copied from interface:ListADT
Returnstrue
if this list contains no elements. -
contains
Description copied from interface:ListADT
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)
.- Specified by:
contains
in interfaceListADT<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
Description copied from interface:ListADT
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.- Specified by:
toArray
in interfaceListADT<E>
- 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
Description copied from interface:ListADT
Returns an array containing all the elements in this list in a proper sequence. Obeys the general contract of thejava.util.Collection.toArray()
method. -
iterator
Description copied from interface:ListADT
Returns an iterator over the elements in this list, in a proper sequence. -
push
Description copied from interface:StackADT
Returns the data of the element to the last position- Specified by:
push
in interfaceStackADT<E>
- Parameters:
element
- The element to be pushed onto the stack- Throws:
NullPointerException
- If the specified element is null
-
pop
Removes the element in the last position- Specified by:
pop
in interfaceStackADT<E>
- Returns:
- E Runs the remove method on the last element
- Throws:
NoSuchElementException
- If this stack is empty
-
peek
Returns the data of the element to the last position- Specified by:
peek
in interfaceStackADT<E>
- Returns:
- Gets the data stored in the last index.
- Throws:
NoSuchElementException
- If this stack is empty
-