Class FixedArrayList<E>
- All Implemented Interfaces:
Iterable<E>,Collection<E>,RandomAccess
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.
- Since:
- 20040912
- Author:
- Antoine Dutot
-
Constructor Summary
Constructors Constructor Description FixedArrayList()FixedArrayList(int capacity) -
Method Summary
Modifier and Type Method Description booleanadd(E element)Add oneelementin the array.booleanaddAll(Collection<? extends E> c)voidclear()booleancontains(Object o)booleancontainsAll(Collection<?> c)booleanequals(Object o)Eget(int i)I-th element.intgetLastIndex()Last index used by theadd(Object)method.intgetNextAddIndex()The index that will be used in case of a next insertion in this array.booleanisEmpty()Iterator<E>iterator()intrealSize()Real size of the array, counting elements that have been erased.Eremove(int i)Remove the element at indexi.booleanremove(Object e)Remove the elemente.booleanremoveAll(Collection<?> c)booleanretainAll(Collection<?> c)intsize()Number of elements in the array.Object[]toArray()<T> T[]toArray(T[] a)EunsafeGet(int i)I-th element.Methods inherited from class java.lang.Object
getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface java.util.Collection
hashCode, parallelStream, removeIf, spliterator, stream, toArray
-
Constructor Details
-
FixedArrayList
public FixedArrayList() -
FixedArrayList
public FixedArrayList(int capacity)
-
-
Method Details
-
size
public int size()Number of elements in the array.- Specified by:
sizein interfaceCollection<E>- Returns:
- The number of elements in the array.
-
realSize
public int realSize()Real size of the array, counting elements that have been erased.- See Also:
unsafeGet(int)
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<E>
-
get
I-th element.- Parameters:
i- The element index.- Returns:
- The element at index
i.
-
unsafeGet
I-th element. Like theget(int)method but it does not check the element does not exists at the given index.- Parameters:
i- The element index.- Returns:
- The element at index
i.
-
contains
- Specified by:
containsin interfaceCollection<E>
-
containsAll
- Specified by:
containsAllin interfaceCollection<E>
-
equals
- Specified by:
equalsin interfaceCollection<E>- Overrides:
equalsin classObject
-
iterator
-
getLastIndex
public int getLastIndex()Last index used by theadd(Object)method.- Returns:
- The last insertion index.
-
getNextAddIndex
public int getNextAddIndex()The index that will be used in case of a next insertion in this array.- Returns:
-
toArray
- Specified by:
toArrayin interfaceCollection<E>
-
toArray
public <T> T[] toArray(T[] a)- Specified by:
toArrayin interfaceCollection<E>
-
add
Add oneelementin the array. The index used for inserting the element is then available usinggetLastIndex().- Specified by:
addin interfaceCollection<E>- Parameters:
element- The element to add.- Returns:
- Always true.
- Throws:
NullPointerException- If a null value is inserted.- See Also:
getLastIndex()
-
addAll
- Specified by:
addAllin interfaceCollection<E>- Throws:
UnsupportedOperationException
-
remove
Remove the element at indexi.- Parameters:
i- Index of the element to remove.- Returns:
- The removed element.
-
remove
Remove the elemente.- Specified by:
removein interfaceCollection<E>- Parameters:
e- The element to remove.- Returns:
- True if removed.
-
removeAll
- Specified by:
removeAllin interfaceCollection<E>
-
retainAll
- Specified by:
retainAllin interfaceCollection<E>
-
clear
public void clear()- Specified by:
clearin interfaceCollection<E>
-