Class ShrinkableArrayList<E>
- Type Parameters:
E
- the type of the elements in this list
- All Implemented Interfaces:
Iterable<E>
,Collection<E>
,List<E>
,RandomAccess
,SequencedCollection<E>
List
implementation, that has a custom capacity and allows
custom resizing. Implements all optional list operations and permits all elements, including
null
. The main feature of this list is, that it can be resized to a smaller
size. After resizing, the Array behaves as if it was of the new size and all
elements after the new size are removed. Additionally, the list is capped, meaning that
it can never store more elements than its fixed capacity.
The resize
, add
, addLast
, size
, isEmpty
,
get
, set
, getFirst
, getLast
, removeLast
,
iterator
, listIterator
, and reversed
operations run in
constant time. All the operations run in linear time (roughly speaking).
Each CappedResizableList
instance has a fixed capacity. The capacity
is the maximum number of elements the list can store. However, the list does not
need to store that many elements. The actual amount of elements stored in the list
is called the size of the list. The size of the list is always less than or
equal to the capacity. Unlike the capacity, the size of the list isn't
fixed. It changes when elements are added or removed from the list or when the list
is resized.
Like the ArrayList
, the CappedResizableList
is not synchronized
and its iterator is fail-fast. Read the documentation of the ArrayList
for more
information about these topics.
- Author:
- Paul Steinbach
- See Also:
-
Field Summary
Fields inherited from class java.util.AbstractList
modCount
-
Constructor Summary
ConstructorDescriptionConstructs a newCappedResizableList
with the specified capacity.ShrinkableArrayList
(Collection<? extends E> collection) ShrinkableArrayList
(ShrinkableArrayList<? extends E> shrinkableArrayList) -
Method Summary
Methods inherited from class java.util.AbstractList
add, clear, equals, hashCode, indexOf, iterator, lastIndexOf, listIterator, listIterator, removeRange, subList
Methods inherited from class java.util.AbstractCollection
addAll, contains, containsAll, isEmpty, remove, removeAll, retainAll, toArray, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
Methods inherited from interface java.util.List
add, addAll, addFirst, addLast, clear, contains, containsAll, equals, getFirst, getLast, hashCode, indexOf, isEmpty, iterator, lastIndexOf, listIterator, listIterator, remove, removeAll, removeFirst, replaceAll, retainAll, reversed, sort, spliterator, subList, toArray
-
Constructor Details
-
ShrinkableArrayList
public ShrinkableArrayList()Constructs a newCappedResizableList
with the specified capacity.- Throws:
IllegalArgumentException
- if the capacity is negative
-
ShrinkableArrayList
-
ShrinkableArrayList
-
-
Method Details
-
shrink
public void shrink(int newSize) Resizes the list to a new smaller size. All elements at indices beyond the new size won't be visible anymore.- Parameters:
newSize
- the new size of the list- Throws:
IllegalArgumentException
- if the new size is negative or greater than the current size
-
get
-
size
public int size()- Specified by:
size
in interfaceCollection<E>
- Specified by:
size
in interfaceList<E>
- Specified by:
size
in classAbstractCollection<E>
-
set
-
add
-
remove
-
removeLast
- Specified by:
removeLast
in interfaceList<E>
- Specified by:
removeLast
in interfaceSequencedCollection<E>
-
toArray
- Specified by:
toArray
in interfaceCollection<E>
- Specified by:
toArray
in interfaceList<E>
- Overrides:
toArray
in classAbstractCollection<E>
-
addAll
-