public interface List4D<E>
extends java.util.Collection<E>
List places additional requirements on iterator
,
add
, remove
, equals
, and
hashCode
, in addition to requiring more methods. List
indexing is 0-based (like arrays), although some implementations may
require time proportional to the index to obtain an arbitrary element.
The List interface is incompatible with Set; you cannot implement both
simultaneously.
Lists also provide a ListIterator
which allows bidirectional
traversal and other features atop regular iterators. Lists can be
searched for arbitrary elements, and allow easy insertion and removal
of multiple elements in one method call.
Note: While lists may contain themselves as elements, this leads to undefined (usually infinite recursive) behavior for some methods like hashCode or equals.
Collection
,
Set
,
ArrayList
,
LinkedList
,
Vector
,
Arrays.asList(Object[])
,
Collections.nCopies(int, Object)
,
Collections.EMPTY_LIST
,
AbstractList
,
AbstractSequentialList
Modifier and Type | Method and Description |
---|---|
boolean |
add(E o)
Add an element to the end of the list (optional operation).
|
void |
add(int index,
E o)
Insert an element into the list at a given position (optional operation).
|
boolean |
addAll(java.util.Collection<? extends E> c)
Add the contents of a collection to the end of the list (optional
operation).
|
boolean |
addAll(int index,
java.util.Collection<? extends E> c)
Insert the contents of a collection into the list at a given position
(optional operation).
|
void |
clear()
Clear the list, such that a subsequent call to isEmpty() would return
true (optional operation).
|
boolean |
contains(java.lang.Object o)
Test whether this list contains a given object as one of its elements.
|
boolean |
containsAll(java.util.Collection<?> c)
Test whether this list contains every element in a given collection.
|
boolean |
equals(java.lang.Object o)
Test whether this list is equal to another object.
|
E |
get(int index)
Get the element at a given index in this list.
|
int |
hashCode()
Obtains a hash code for this list.
|
int |
indexOf(java.lang.Object o)
Obtain the first index at which a given object is to be found in this
list.
|
boolean |
isEmpty()
Test whether this list is empty, that is, if size() == 0.
|
java.util.Iterator<E> |
iterator()
Obtain an Iterator over this list, whose sequence is the list order.
|
int |
lastIndexOf(java.lang.Object o)
Obtain the last index at which a given object is to be found in this
list.
|
java.util.ListIterator<E> |
listIterator()
Obtain a ListIterator over this list, starting at the beginning.
|
java.util.ListIterator<E> |
listIterator(int index)
Obtain a ListIterator over this list, starting at a given position.
|
E |
remove(int index)
Remove the element at a given position in this list (optional operation).
|
boolean |
remove(java.lang.Object o)
Remove the first occurence of an object from this list (optional
operation).
|
boolean |
removeAll(java.util.Collection<?> c)
Remove all elements of a given collection from this list (optional
operation).
|
boolean |
retainAll(java.util.Collection<?> c)
Remove all elements of this list that are not contained in a given
collection (optional operation).
|
E |
set(int index,
E o)
Replace an element of this list with another object (optional operation).
|
int |
size()
Get the number of elements in this list.
|
java.util.List<E> |
subList(int fromIndex,
int toIndex)
Obtain a List view of a subsection of this list, from fromIndex
(inclusive) to toIndex (exclusive).
|
java.lang.Object[] |
toArray()
Copy the current contents of this list into an array.
|
<T> T[] |
toArray(T[] a)
Copy the current contents of this list into an array.
|
void add(int index, E o)
index
- the location to insert the itemo
- the object to insertjava.lang.UnsupportedOperationException
- if this list does not support the
add operationjava.lang.IndexOutOfBoundsException
- if index < 0 || index > size()java.lang.ClassCastException
- if o cannot be added to this list due to its
typejava.lang.IllegalArgumentException
- if o cannot be added to this list for
some other reasonjava.lang.NullPointerException
- if o is null and this list doesn't support
the addition of null values.boolean add(E o)
add
in interface java.util.Collection<E>
o
- the object to addjava.lang.UnsupportedOperationException
- if this list does not support the
add operationjava.lang.ClassCastException
- if o cannot be added to this list due to its
typejava.lang.IllegalArgumentException
- if o cannot be added to this list for
some other reasonjava.lang.NullPointerException
- if o is null and this list doesn't support
the addition of null values.boolean addAll(int index, java.util.Collection<? extends E> c)
index
- the location to insert the collectionc
- the collection to insertjava.lang.UnsupportedOperationException
- if this list does not support the
addAll operationjava.lang.IndexOutOfBoundsException
- if index < 0 || index > size()java.lang.ClassCastException
- if some element of c cannot be added to this
list due to its typejava.lang.IllegalArgumentException
- if some element of c cannot be added
to this list for some other reasonjava.lang.NullPointerException
- if some element of c is null and this list
doesn't support the addition of null values.java.lang.NullPointerException
- if the specified collection is nulladd(int, Object)
boolean addAll(java.util.Collection<? extends E> c)
addAll
in interface java.util.Collection<E>
c
- the collection to addjava.lang.UnsupportedOperationException
- if this list does not support the
addAll operationjava.lang.ClassCastException
- if some element of c cannot be added to this
list due to its typejava.lang.IllegalArgumentException
- if some element of c cannot be added
to this list for some other reasonjava.lang.NullPointerException
- if the specified collection is nulljava.lang.NullPointerException
- if some element of c is null and this list
doesn't support the addition of null values.add(Object)
void clear()
clear
in interface java.util.Collection<E>
java.lang.UnsupportedOperationException
- if this list does not support the
clear operationboolean contains(java.lang.Object o)
o == null ? e == null : o.equals(e)
.contains
in interface java.util.Collection<E>
o
- the element to look forjava.lang.ClassCastException
- if the type of o is not a valid type
for this list.java.lang.NullPointerException
- if o is null and the list doesn't
support null values.boolean containsAll(java.util.Collection<?> c)
containsAll
in interface java.util.Collection<E>
c
- the collection to test forjava.lang.NullPointerException
- if the collection is nulljava.lang.ClassCastException
- if the type of any element in c is not a valid
type for this list.java.lang.NullPointerException
- if some element of c is null and this
list does not support null values.contains(Object)
boolean equals(java.lang.Object o)
l1.size() == l2.size()
, and for every integer n between 0
and l1.size() - 1
inclusive, l1.get(n) == null ?
l2.get(n) == null : l1.get(n).equals(l2.get(n))
.equals
in interface java.util.Collection<E>
equals
in class java.lang.Object
o
- the object to test for equality with this listObject.equals(Object)
,
hashCode()
E get(int index)
index
- the index of the element to be returnedjava.lang.IndexOutOfBoundsException
- if index < 0 || index >= size()int hashCode()
hashCode = 1; Iterator i = list.iterator(); while (i.hasNext()) { Object obj = i.next(); hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode()); }
This ensures that the general contract of Object.hashCode() is adhered to.
hashCode
in interface java.util.Collection<E>
hashCode
in class java.lang.Object
Object.hashCode()
,
equals(Object)
int indexOf(java.lang.Object o)
o
- the object to search foro == null ? get(n) == null :
o.equals(get(n))
, or -1 if there is no such index.java.lang.ClassCastException
- if the type of o is not a valid
type for this list.java.lang.NullPointerException
- if o is null and this
list does not support null values.boolean isEmpty()
isEmpty
in interface java.util.Collection<E>
java.util.Iterator<E> iterator()
int lastIndexOf(java.lang.Object o)
o == null ? get(n) == null
: o.equals(get(n))
, or -1 if there is no such index.java.lang.ClassCastException
- if the type of o is not a valid
type for this list.java.lang.NullPointerException
- if o is null and this
list does not support null values.java.util.ListIterator<E> listIterator()
java.util.ListIterator<E> listIterator(int index)
index
- the position, between 0 and size() inclusive, to begin the
iteration fromjava.lang.IndexOutOfBoundsException
- if index < 0 || index > size()E remove(int index)
index
- the position within the list of the object to removejava.lang.UnsupportedOperationException
- if this list does not support the
remove operationjava.lang.IndexOutOfBoundsException
- if index < 0 || index >= size()boolean remove(java.lang.Object o)
o == null ? e == null : o.equals(e)
.remove
in interface java.util.Collection<E>
o
- the object to removejava.lang.UnsupportedOperationException
- if this list does not support the
remove operationjava.lang.ClassCastException
- if the type of o is not a valid
type for this list.java.lang.NullPointerException
- if o is null and this
list does not support removing null values.boolean removeAll(java.util.Collection<?> c)
removeAll
in interface java.util.Collection<E>
c
- the collection to filter outjava.lang.UnsupportedOperationException
- if this list does not support the
removeAll operationjava.lang.NullPointerException
- if the collection is nulljava.lang.ClassCastException
- if the type of any element in c is not a valid
type for this list.java.lang.NullPointerException
- if some element of c is null and this
list does not support removing null values.remove(Object)
,
contains(Object)
boolean retainAll(java.util.Collection<?> c)
retainAll
in interface java.util.Collection<E>
c
- the collection to retainjava.lang.UnsupportedOperationException
- if this list does not support the
retainAll operationjava.lang.NullPointerException
- if the collection is nulljava.lang.ClassCastException
- if the type of any element in c is not a valid
type for this list.java.lang.NullPointerException
- if some element of c is null and this
list does not support retaining null values.remove(Object)
,
contains(Object)
E set(int index, E o)
index
- the position within this list of the element to be replacedo
- the object to replace it withjava.lang.UnsupportedOperationException
- if this list does not support the
set operationjava.lang.IndexOutOfBoundsException
- if index < 0 || index >= size()java.lang.ClassCastException
- if o cannot be added to this list due to its
typejava.lang.IllegalArgumentException
- if o cannot be added to this list for
some other reasonjava.lang.NullPointerException
- if o is null and this
list does not support null values.int size()
size
in interface java.util.Collection<E>
java.util.List<E> subList(int fromIndex, int toIndex)
fromIndex
- the index that the returned list should start from
(inclusive)toIndex
- the index that the returned list should go to (exclusive)java.lang.IndexOutOfBoundsException
- if fromIndex < 0
|| toIndex > size() || fromIndex > toIndexjava.lang.Object[] toArray()
toArray
in interface java.util.Collection<E>
<T> T[] toArray(T[] a)
toArray
in interface java.util.Collection<E>
a
- the array to copy this list intojava.lang.ArrayStoreException
- if the type of any element of the
collection is not a subtype of the element type of ajava.lang.NullPointerException
- if the specified array is null