Interface MyList<E>
-
public interface MyList<E>
An interface specifying the operations that should be supported by a simple list data structure.- Version:
- 1.1
- Author:
- Rui Meireles
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description void
add(int idx, E elem)
Adds a new element to the list at the index specified as an argument.boolean
add(E elem)
Appends a new element to the end of the list.E
get(int idx)
Returns the list element stored at the index specified as an argument.int
indexOf(E elem)
Returns the index at which the element passed as an argument can be found on the list.E
remove(int idx)
Removes and returns the list element located at the index specified as an argument.int
size()
Returns the number of elements on the list.java.lang.String
toString()
Returns a textual representation of the list elements in the format "{elem1, elem2, .., elemn}".
-
-
-
Method Detail
-
size
int size()
Returns the number of elements on the list.- Returns:
- the number of elements on the list.
-
add
boolean add(E elem)
Appends a new element to the end of the list.- Parameters:
elem
- the element to add to the list.- Returns:
- always returns true.
-
add
void add(int idx, E elem) throws java.lang.IndexOutOfBoundsException
Adds a new element to the list at the index specified as an argument.- Parameters:
idx
- the index at which the new element should be found once it has been added to the list.E
- elem the element to add to the list.- Throws:
java.lang.IndexOutOfBoundsException
- if index not in range [0,size()].
-
remove
E remove(int idx) throws java.lang.IndexOutOfBoundsException
Removes and returns the list element located at the index specified as an argument.- Parameters:
idx
- the index at which the element to be removed resides.- Returns:
- the element removed.
- Throws:
java.lang.IndexOutOfBoundsException
-
get
E get(int idx) throws java.lang.IndexOutOfBoundsException
Returns the list element stored at the index specified as an argument.- Parameters:
idx
- the index of the element to be retrieved.- Returns:
- the element at the specified index.
- Throws:
java.lang.IndexOutOfBoundsException
-
indexOf
int indexOf(E elem)
Returns the index at which the element passed as an argument can be found on the list. Equality is determined by the equals(Object o) method, defined in the Object class, and potentially overridden by the element's class. If the elem appears multiple times on the list, the lowest index at which it can be found is returned. If the element is not present in the list, -1 is returned.- Parameters:
elem
- the element we're trying to find.- Returns:
- the index of the first ocurrence of elemement elem, or -1 if not found.
-
toString
java.lang.String toString()
Returns a textual representation of the list elements in the format "{elem1, elem2, .., elemn}". E.g. A list with elements 1, 2 and 3 should return "{1, 2, 3}".- Overrides:
toString
in classjava.lang.Object
- Returns:
- the string representation of the list.
-
-