E
- the type of elements in this listjava.lang.Cloneable
, java.lang.Iterable<E>
, java.util.Collection<E>
, java.util.List<E>
public class MyList<E>
extends java.util.AbstractList<E>
implements java.util.List<E>, java.lang.Cloneable
Modifier and Type | Class | Description |
---|---|---|
private class |
MyList.Itr |
An optimized version of AbstractList.Itr
|
Modifier and Type | Field | Description |
---|---|---|
private static int |
DEFAULT_CAPACITY |
Default initial capacity.
|
private static java.lang.Object[] |
DEFAULTCAPACITY_EMPTY_ELEMENTDATA |
Shared empty array instance used for default sized empty instances.
|
(package private) java.lang.Object[] |
elementData |
The array buffer into which the elements of the MyList are stored.
|
private static java.lang.Object[] |
EMPTY_ELEMENTDATA |
Shared empty array instance used for empty instances.
|
private static int |
MAX_ARRAY_SIZE |
The maximum size of array to allocate (unless necessary).
|
private int |
size |
The size of the MyList (the number of elements it contains).
|
SortI |
sortI |
Sorted
|
Constructor | Description |
---|---|
MyList() |
Constructs an empty list with an initial capacity of ten.
|
MyList(java.util.Collection<? extends E> c) |
Constructs a list containing the elements of the specified
collection, in the order they are returned by the collection's
iterator.
|
Modifier and Type | Method | Description |
---|---|---|
void |
add(int index,
E element) |
Inserts the specified element at the specified position in this
list and sorts.
|
boolean |
add(E e) |
Appends the specified element to the end of this list and sorts.
|
private void |
add(E e,
java.lang.Object[] elementData,
int s) |
This helper method split out from add(E) to keep method
bytecode size under 35 (the -XX:MaxInlineSize default value),
which helps when add(E) is called in a C1-compiled loop.
|
boolean |
addAll(int index,
java.util.Collection<? extends E> c) |
Inserts all of the elements in the specified collection into this
list, starting at the specified position.
|
boolean |
addAll(java.util.Collection<? extends E> c) |
Appends all of the elements in the specified collection to the end of
this list, in the order that they are returned by the
specified collection's Iterator.
|
void |
clear() |
Removes all of the elements from this list.
|
java.lang.Object |
clone() |
Returns a shallow copy of this
MyList instance. |
boolean |
contains(java.lang.Object o) |
Returns
true if this list contains the specified element. |
(package private) static <E> E |
elementAt(java.lang.Object[] es,
int index) |
|
(package private) E |
elementData(int index) |
|
private void |
fastRemove(int index) |
Private remove method that skips bounds checking and does not
return the value removed.
|
E |
get(int index) |
Returns the element at the specified position in this list.
|
SortI |
getSortI() |
|
private java.lang.Object[] |
grow() |
|
private java.lang.Object[] |
grow(int minCapacity) |
Increases the capacity to ensure that it can hold at least the
number of elements specified by the minimum capacity argument.
|
private static int |
hugeCapacity(int minCapacity) |
|
int |
indexOf(java.lang.Object o) |
Returns the index of the first occurrence of the specified element
in this list, or -1 if this list does not contain the element.
|
boolean |
isEmpty() |
Returns
true if this list contains no elements. |
java.util.Iterator<E> |
iterator() |
Returns an iterator over the elements in this list in proper sequence.
|
int |
lastIndexOf(java.lang.Object o) |
Returns the index of the last occurrence of the specified element
in this list, or -1 if this list does not contain the element.
|
private int |
newCapacity(int minCapacity) |
Returns a capacity at least as large as the given minimum capacity.
|
private java.lang.String |
outOfBoundsMsg(int index) |
Constructs an IndexOutOfBoundsException detail message.
|
private void |
rangeCheckForAdd(int index) |
A version of rangeCheck used by add and addAll.
|
E |
remove(int index) |
Removes the element at the specified position in this list.
|
boolean |
remove(java.lang.Object o) |
Removes the first occurrence of the specified element from this list,
if it is present.
|
E |
set(int index,
E element) |
Removes an element by index,
and adds newRemoves an element by index,
and adds new
|
void |
setSortI(SortI sortI) |
|
int |
size() |
Returns the number of elements in this list.
|
private void |
sorts() |
Sorts MyList
|
java.lang.Object[] |
toArray() |
Returns an array containing all of the elements in this list
in proper sequence (from first to last element).
|
<T> T[] |
toArray(T[] a) |
Returns an array containing all of the elements in this list in proper
sequence (from first to last element); the runtime type of the returned
array is that of the specified array.
|
containsAll, removeAll, retainAll, toString
equals, hashCode, listIterator, listIterator, removeRange, subList
private static final int DEFAULT_CAPACITY
private static final java.lang.Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA
transient java.lang.Object[] elementData
private static final java.lang.Object[] EMPTY_ELEMENTDATA
private static final int MAX_ARRAY_SIZE
private int size
public SortI sortI
public MyList()
public MyList(java.util.Collection<? extends E> c)
c
- the collection whose elements are to be placed into this listjava.lang.NullPointerException
- if the specified collection is nullpublic void add(int index, E element)
public boolean add(E e)
private void add(E e, java.lang.Object[] elementData, int s)
public boolean addAll(int index, java.util.Collection<? extends E> c)
addAll
in interface java.util.List<E>
addAll
in class java.util.AbstractList<E>
index
- index at which to insert the first element from the
specified collectionc
- collection containing elements to be added to this listtrue
if this list changed as a result of the calljava.lang.IndexOutOfBoundsException
java.lang.NullPointerException
- if the specified collection is nullpublic boolean addAll(java.util.Collection<? extends E> c)
addAll
in interface java.util.Collection<E>
addAll
in interface java.util.List<E>
addAll
in class java.util.AbstractCollection<E>
c
- collection containing elements to be added to this listtrue
if this list changed as a result of the calljava.lang.NullPointerException
- if the specified collection is nullpublic void clear()
public java.lang.Object clone()
MyList
instance. (The
elements themselves are not copied.)clone
in class java.lang.Object
MyList
instancepublic boolean contains(java.lang.Object o)
true
if this list contains the specified element.
More formally, returns true
if and only if this list contains
at least one element e
such that
Objects.equals(o, e)
.contains
in interface java.util.Collection<E>
contains
in interface java.util.List<E>
contains
in class java.util.AbstractCollection<E>
o
- element whose presence in this list is to be testedtrue
if this list contains the specified elementstatic <E> E elementAt(java.lang.Object[] es, int index)
E elementData(int index)
private void fastRemove(int index)
public E get(int index)
public SortI getSortI()
private java.lang.Object[] grow()
private java.lang.Object[] grow(int minCapacity)
minCapacity
- the desired minimum capacityjava.lang.OutOfMemoryError
- if minCapacity is less than zeroprivate static int hugeCapacity(int minCapacity)
public int indexOf(java.lang.Object o)
i
such that
Objects.equals(o, get(i))
,
or -1 if there is no such index.public boolean isEmpty()
true
if this list contains no elements.public java.util.Iterator<E> iterator()
The returned iterator is fail-fast.
public int lastIndexOf(java.lang.Object o)
i
such that
Objects.equals(o, get(i))
,
or -1 if there is no such index.private int newCapacity(int minCapacity)
minCapacity
- the desired minimum capacityjava.lang.OutOfMemoryError
- if minCapacity is less than zeroprivate java.lang.String outOfBoundsMsg(int index)
private void rangeCheckForAdd(int index)
public E remove(int index)
public boolean remove(java.lang.Object o)
i
such that
Objects.equals(o, get(i))
(if such an element exists). Returns true
if this list
contained the specified element (or equivalently, if this list
changed as a result of the call).public E set(int index, E element)
set
in interface java.util.List<E>
set
in class java.util.AbstractList<E>
index
- index of the element to replaceelement
- element to be stored at the specified positionjava.lang.IndexOutOfBoundsException
public void setSortI(SortI sortI)
public int size()
private void sorts()
public java.lang.Object[] toArray()
The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
public <T> T[] toArray(T[] a)
If the list fits in the specified array with room to spare
(i.e., the array has more elements than the list), the element in
the array immediately following the end of the collection is set to
null
. (This is useful in determining the length of the
list only if the caller knows that the list does not contain
any null elements.)
toArray
in interface java.util.Collection<E>
toArray
in interface java.util.List<E>
toArray
in class java.util.AbstractCollection<E>
a
- the array into which the elements of the list are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose.java.lang.ArrayStoreException
- if the runtime type of the specified array
is not a supertype of the runtime type of every element in
this listjava.lang.NullPointerException
- if the specified array is null