public class SortedArrayList<E>
extends java.util.ArrayList<E>
This class extends ArrayList to support sorted lists.
All elements of the list should implement the Comparable interface, or you may supply a custom Comparator instead. If the objects do not support comparable and you do not supply a custom Comparator, the result of the toString() function for each object is used.
The ArrayList methods for adding or settings elements at specific indices are not supported raise an UnsupportedOperationException if used.
Modifier and Type | Field and Description |
---|---|
protected java.util.Comparator<E> |
comparator
Optional Comparator used for comparing objects in the list.
|
Constructor and Description |
---|
SortedArrayList()
Create empty sorted array list.
|
SortedArrayList(java.util.Collection<? extends E> collection)
Create sorted array list from a collection.
|
SortedArrayList(java.util.Collection<? extends E> collection,
java.util.Comparator<E> comparator)
Create sorted array list from a collection with specified comparator.
|
SortedArrayList(java.util.Comparator<E> comparator)
Create empty sorted array list with specified Comparator.
|
SortedArrayList(E[] array)
Create sorted array list from an array.
|
SortedArrayList(E[] array,
java.util.Comparator<E> comparator)
Create sorted array list from an array with specified comparator.
|
SortedArrayList(int initialCapacity)
Create empty sorted array list with specified initial capacity.
|
SortedArrayList(int initialCapacity,
java.util.Comparator<E> comparator)
Create empty sorted array list with specified initial capacity
and comparator.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E object)
Add an object to the list.
|
void |
add(int index,
E object)
Add element at specified index.
|
boolean |
addAll(java.util.Collection<? extends E> collection)
Add all elements of a collection.
|
boolean |
addAll(E[] array)
Add all elements of an array.
|
boolean |
addAll(int index,
java.util.Collection<? extends E> collection)
Add all elements of a collection at a specified index.
|
protected int |
compare(java.lang.Object object1,
java.lang.Object object2)
Compares two elements.
|
boolean |
contains(java.lang.Object object)
Determine if list contains a specified value.
|
int |
indexOf(java.lang.Object object)
Return index of first matching list entry.
|
int |
lastIndexOf(java.lang.Object object)
Return index of last matching list entry.
|
E |
set(int index,
E object)
Set specified element of list.
|
void |
setComparator(java.util.Comparator<E> comparator)
Set comparator for list elements.
|
protected void |
sort()
Sort list.
|
clear, clone, ensureCapacity, get, isEmpty, iterator, listIterator, listIterator, remove, remove, removeAll, removeRange, retainAll, size, subList, toArray, toArray, trimToSize
protected java.util.Comparator<E> comparator
public SortedArrayList()
public SortedArrayList(int initialCapacity)
public SortedArrayList(java.util.Collection<? extends E> collection)
collection
- The source collection.public SortedArrayList(E[] array)
array
- The source array.public SortedArrayList(java.util.Comparator<E> comparator)
public SortedArrayList(java.util.Collection<? extends E> collection, java.util.Comparator<E> comparator)
collection
- The source collection.comparator
- The comparator.public SortedArrayList(E[] array, java.util.Comparator<E> comparator)
array
- The source array.comparator
- The comparator.public SortedArrayList(int initialCapacity, java.util.Comparator<E> comparator)
public void setComparator(java.util.Comparator<E> comparator)
comparator
- The comparator.public boolean add(E object)
public void add(int index, E object)
public boolean addAll(java.util.Collection<? extends E> collection)
public boolean addAll(E[] array)
array
- The source array.public boolean addAll(int index, java.util.Collection<? extends E> collection)
public boolean contains(java.lang.Object object)
public int indexOf(java.lang.Object object)
public int lastIndexOf(java.lang.Object object)
protected int compare(java.lang.Object object1, java.lang.Object object2)
object1
- First object.object2
- Second object.Assumes the two objects are not null, since null objects cannot added to the list. If no comparator is defined for this SortedArrayList, and one or both of the objects are not comparable, a case-ignoring comparison is performed on the "toString()" values for each object.
protected void sort()