Package com.zybooks.dsaj.list
Interface List<E>
- Type Parameters:
E
- the element type
- All Superinterfaces:
Iterable<E>
- All Known Implementing Classes:
ArrayList
A simplified version of the java.util.List interface.
-
Method Summary
Modifier and TypeMethodDescriptionvoid
Inserts the given element at the specified index of the list, shifting all subsequent elements in the list one position further to make room.get
(int i) Returns (but does not remove) the element at index i.boolean
isEmpty()
Tests whether the list is empty.iterator()
Returns an iterator of the elements stored in the list.remove
(int i) Removes and returns the element at the given index, shifting all subsequent elements in the list one position closer to the front.Replaces the element at the specified index, and returns the element previously stored.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
-
get
Returns (but does not remove) the element at index i.- Parameters:
i
- the index of the element to return- Returns:
- the element at the specified index
- Throws:
IndexOutOfBoundsException
- if the index is negative or greater than size()-1
-
set
Replaces the element at the specified index, and returns the element previously stored.- Parameters:
i
- the index of the element to replacee
- the new element to be stored- Returns:
- the previously stored element
- Throws:
IndexOutOfBoundsException
- if the index is negative or greater than size()-1
-
add
Inserts the given element at the specified index of the list, shifting all subsequent elements in the list one position further to make room.- Parameters:
i
- the index at which the new element should be storede
- the new element to be stored- Throws:
IndexOutOfBoundsException
- if the index is negative or greater than size()
-
remove
Removes and returns the element at the given index, shifting all subsequent elements in the list one position closer to the front.- Parameters:
i
- the index of the element to be removed- Returns:
- the element that had be stored at the given index
- Throws:
IndexOutOfBoundsException
- if the index is negative or greater than size()
-
iterator
Returns an iterator of the elements stored in the list.
-