public class FixedArrayList<E> extends Object implements Collection<E>, RandomAccess
A fixed array list is like an array list, but it ensures the property that
each element will always stay at the same index, even if elements are
removed in between. The counterpart of this property is that the array
handles by itself the insertion of new elements (since when an element is
removed in the middle, this position can be reused), and therefore indices
cannot be chosen (i.e. only the add(Object)
and
addAll(Collection)
methods are usable to insert new elements in the
array).
This is the reason why this does not implement the List interface, because the add(int,E) method cannot be implemented.
Furthermore, this array cannot contain null values, because it marks unused positions within the array using the null value.
Modifier and Type | Class and Description |
---|---|
protected class |
FixedArrayList.FixedArrayIterator |
Modifier and Type | Field and Description |
---|---|
protected ArrayList<E> |
elements
List of elements.
|
protected ArrayList<Integer> |
freeIndices
List of free indices.
|
protected int |
lastIndex
Last inserted element index.
|
Constructor and Description |
---|
FixedArrayList() |
FixedArrayList(int capacity) |
Modifier and Type | Method and Description |
---|---|
boolean |
add(E element)
Add one
element in the array. |
boolean |
addAll(Collection<? extends E> c) |
void |
clear() |
boolean |
contains(Object o) |
boolean |
containsAll(Collection<?> c) |
boolean |
equals(Object o) |
E |
get(int i)
I-th element.
|
int |
getLastIndex()
Last index used by the
add(Object) method. |
int |
getNextAddIndex()
The index that will be used in case of a next insertion in this array.
|
boolean |
isEmpty() |
Iterator<E> |
iterator() |
int |
realSize()
Real size of the array, counting elements that have been erased.
|
E |
remove(int i)
Remove the element at index
i . |
boolean |
remove(Object e)
Remove the element
e . |
boolean |
removeAll(Collection<?> c) |
protected void |
removeIt(int i) |
boolean |
retainAll(Collection<?> c) |
int |
size()
Number of elements in the array.
|
Object[] |
toArray() |
<T> T[] |
toArray(T[] a) |
E |
unsafeGet(int i)
I-th element.
|
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
hashCode, parallelStream, removeIf, spliterator, stream
protected ArrayList<Integer> freeIndices
protected int lastIndex
public FixedArrayList()
public FixedArrayList(int capacity)
public int size()
size
in interface Collection<E>
public int realSize()
unsafeGet(int)
public boolean isEmpty()
isEmpty
in interface Collection<E>
public E get(int i)
i
- The element index.i
.public E unsafeGet(int i)
get(int)
method but it does not check
the element does not exists at the given index.i
- The element index.i
.public boolean contains(Object o)
contains
in interface Collection<E>
public boolean containsAll(Collection<?> c)
containsAll
in interface Collection<E>
public boolean equals(Object o)
equals
in interface Collection<E>
equals
in class Object
public int getLastIndex()
add(Object)
method.public int getNextAddIndex()
public Object[] toArray()
toArray
in interface Collection<E>
public <T> T[] toArray(T[] a)
toArray
in interface Collection<E>
public boolean add(E element) throws NullPointerException
element
in the array. The index used for inserting
the element is then available using getLastIndex()
.add
in interface Collection<E>
element
- The element to add.NullPointerException
- If a null value is inserted.getLastIndex()
public boolean addAll(Collection<? extends E> c) throws UnsupportedOperationException
addAll
in interface Collection<E>
UnsupportedOperationException
public E remove(int i)
i
.i
- Index of the element to remove.protected void removeIt(int i)
public boolean remove(Object e)
e
.remove
in interface Collection<E>
e
- The element to remove.public boolean removeAll(Collection<?> c)
removeAll
in interface Collection<E>
public boolean retainAll(Collection<?> c)
retainAll
in interface Collection<E>
public void clear()
clear
in interface Collection<E>
WebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses