Package examples
Interface List<E>
public interface List<E>
An ordered, indexed list.
List element positions are indexed starting from 0. Negative positions may also be used, in which case the value will be interpreted as an offset from the end of the list. For example, the final element in any non-empty list may be accessed at the index -1.
-
Method Summary
Modifier and Type Method Description voidadd(E element)Add the given element to the front of this list.voidadd(E element, int index)Inserts an element at the given position.voidclear()Remove all elements from this collection.List<E>cut(int index)Splits the list in two at the given position.Efirst()Get the element stored at the beginning of the list.Eget(int index)Get the element stored at the given position in the list.intindexOf(E element)Get the position of the first occurrence of the given element.Elast()Get the element stored at the end of list.Eremove(int index)Removes the element at the given position from the list and returns it.voidreverse()Reverses the order of all elements in this list.intsize()Returns the number of elements in the list.
-
Method Details
-
size
int size()Returns the number of elements in the list.- Returns:
- total number of elements stored in this collection.
-
clear
void clear()Remove all elements from this collection. -
first
Get the element stored at the beginning of the list. This element is not removed from the collection.- Returns:
- the first element stored in this collection.
- Throws:
NoSuchElementException- if the list is empty.
-
last
Get the element stored at the end of list. This element is not removed from the collection.- Returns:
- the last element stored in this collection.
- Throws:
NoSuchElementException- if the list is empty
-
get
Get the element stored at the given position in the list.- Parameters:
index- position of the element to retrieve- Returns:
- the element at the given position
- Throws:
IndexOutOfBoundsException- ifindex ∉ [-size(), size())
-
add
Add the given element to the front of this list.- Parameters:
element- element to be added
-
add
Inserts an element at the given position.- Parameters:
element- element to be addedindex- position at which the new element will be inserted- Throws:
IndexOutOfBoundsException- ifindex ∉ [-size(), size()]
-
remove
Removes the element at the given position from the list and returns it.- Parameters:
index- position of the element to be removed- Returns:
- the element that was removed
- Throws:
IndexOutOfBoundsException- ifindex ∉ [-size(), size())
-
indexOf
Get the position of the first occurrence of the given element. Elements in the collection will be compared to the argument usingObject.equals(Object).- Parameters:
element- the element to search for- Returns:
- position index
- Throws:
NoSuchElementException- if the givenelementis not stored in this collection.
-
reverse
void reverse()Reverses the order of all elements in this list. -
cut
Splits the list in two at the given position. The element at that position and all following elements will be removed from this list and returned as part of a new list.- Parameters:
index- position of the first element to be removed- Returns:
- a new list containing all elements that were removed
- Throws:
IndexOutOfBoundsException- ifindex ∉ [-size(), size())
-