Java Reference
In-Depth Information
public
T remove(
int
index)
(Optional)
Removes the element at position
index
in the calling object. Shifts any subsequent elements to the
left (subtracts one from their indices). Returns the element that was removed from the calling object.
Throws:
UnsupportedOperationException
if the
remove
method is not supported by the
calling object.
IndexOutOfBoundsException
if
index
does not satisfy:
0 <= index < size()
public
T set(
int
index, T newElement)
(Optional)
Sets the element at the specifi ed
index
to
newElement
. The element previously at that position
is returned.
Throws:
IndexOutOfBoundsException
if the
index
is not in the range:
0 <= index < size()
.
UnsupportedOperationException
if the
set
method is not supported by the calling object.
ClassCastException
if the class of
newElement
prevents it from being added to
the calling object.
NullPointerException
if
newElement
is
null
and the calling object does not support
null
elements.
IllegalArgumentException
if some aspect of
newElement
prevents it from being added to
the calling object.
ListIterator<T> Interface
Package:
java
.util
Ancestor interfaces:
Iterator<T>
The
cursor position
is explained in Chapter 16.
All the exception classes mentioned are the kind that are not required to be caught in a
catch
block or declared in a
throws
clause.
NoSuchElementException
is in the
java
.
util
package, which requires an
import
statement
if your code mentions the
NoSuchElementException
class. All the other exception classes
mentioned are in the package
java.lang
and so do not require any
import
statements.
public void
add(T newElement)
(Optional)
Inserts
newElement
at the location of the iterator cursor (that is, before the value, if any,
that would be returned by
next()
and after the value, if any, that would be returned by
previous()
). Cannot be used if there has been a call to
add
or
remove
since the last call to
next()
or
previous()
.
Throws:
IllegalStateException
if neither
next()
nor
previous()
has been called, or the
add
or
remove
method has already been called after the last call to
next()
or
previous()
.
UnsupportedOperationException
if the
remove
operation is not supported by this
Iterator
.
ClassCastException
if the class of
newElement
prevents it from being added.
IllegalArgumentException
if some property other than the class of
newElement
prevents
it from being added.