Package org.jacop.util
Class SimpleArrayList<V>
java.lang.Object
java.util.AbstractCollection<V>
java.util.AbstractList<V>
org.jacop.util.SimpleArrayList<V>
- Type Parameters:
V- the type which is being stored by this class.
- All Implemented Interfaces:
Iterable<V>,Collection<V>,List<V>,SequencedCollection<V>
Different implementation of an ArrayList data structures. This version is
tailored for JaCoP. Use with care, check when it uses == instead of equals().
- Version:
- 4.10
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate V[]The array buffer into which the elements of the ArrayList are stored.private intThe size of the ArrayList (the number of elements it contains).Fields inherited from class java.util.AbstractList
modCount -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty list with an initial capacity of ten.SimpleArrayList(int initialCapacity) Constructs an empty list with the specified initial capacity.SimpleArrayList(Collection<? extends V> c) Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. -
Method Summary
Modifier and TypeMethodDescriptionvoidInserts the specified element at the specified position in this list.booleanAppends the specified element to the end of this list.booleanaddAll(Collection<? extends V> 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.voidclear()Removes all of the elements from this list.voidsame as clear(), but references to objects are kept internally.booleanReturnstrueif this list contains the specified element.voidensureCapacity(int minCapacity) Increases the capacity of thisArrayListinstance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.booleanAbstractList defines equals so that it depends on the objects included.private voidfastRemove(int index) Private remove method that skips bounds checking and does not return the value removed.get(int index) Returns the element at the specified position in this list.inthashCode()AbstractList defines hashCode so that it depends on the objects included.intSearches for the first occurrence of the given argument, testing for equality using theequalsmethod.intSearches for the first occurrence of the given argument, testing for equality using the == method.booleanisEmpty()Tests if this list has no elements.intlastIndexOf(Object elem) Returns the index of the last occurrence of the specified object in this list.pop()It removes and returns the last element in the list.voidIt inserts the element at the end of the listremove(int index) Removes the element at the specified position in this list.booleanRemoves a single instance of the specified element from this list, if it is present (optional operation).Replaces the element at the specified position in this list with the specified element.voidsetElementAt(V element, int index) Replaces the element at the specified position in this list with the specified element.intsize()Returns the number of elements in this list.Object[]toArray()Returns an array containing all of the elements in this list in the correct order.<T> T[]toArray(T[] a) Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array.toString()Check if the given index is in range.voidTrims the capacity of thisArrayListinstance to be the list's current size.Methods inherited from class java.util.AbstractList
addAll, iterator, listIterator, listIterator, removeRange, subListMethods inherited from class java.util.AbstractCollection
containsAll, removeAll, retainAllMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface java.util.List
addFirst, addLast, containsAll, getFirst, getLast, removeAll, removeFirst, removeLast, replaceAll, retainAll, reversed, sort, spliterator
-
Field Details
-
elementData
The array buffer into which the elements of the ArrayList are stored. The capacity of the ArrayList is the length of this array buffer. -
size
private int sizeThe size of the ArrayList (the number of elements it contains).
-
-
Constructor Details
-
SimpleArrayList
public SimpleArrayList()Constructs an empty list with an initial capacity of ten. -
SimpleArrayList
Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator. TheArrayListinstance has an initial capacity of 110% the size of the specified collection.- Parameters:
c- the collection whose elements are to be placed into this list.- Throws:
NullPointerException- if the specified collection is null.
-
SimpleArrayList
public SimpleArrayList(int initialCapacity) Constructs an empty list with the specified initial capacity.- Parameters:
initialCapacity- the initial capacity of the list.
-
-
Method Details
-
hashCode
public int hashCode()AbstractList defines hashCode so that it depends on the objects included. This makes it costly (linear in the number of elements).Taking the hash of elementData will make it faster TODO make sure this is what we want
- Specified by:
hashCodein interfaceCollection<V>- Specified by:
hashCodein interfaceList<V>- Overrides:
hashCodein classAbstractList<V>
-
equals
AbstractList defines equals so that it depends on the objects included. This makes it costly (linear in the number of elements).Equality of references makes it faster TODO make sure this is what we want
- Specified by:
equalsin interfaceCollection<V>- Specified by:
equalsin interfaceList<V>- Overrides:
equalsin classAbstractList<V>
-
add
Appends the specified element to the end of this list.- Specified by:
addin interfaceCollection<V>- Specified by:
addin interfaceList<V>- Overrides:
addin classAbstractList<V>- Parameters:
o- element to be appended to this list.
-
add
Inserts the specified element at the specified position in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices). -
addAll
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. The behavior of this operation is undefined if the specified Collection is modified while the operation is in progress. (This implies that the behavior of this call is undefined if the specified Collection is this list, and this list is nonempty.)- Specified by:
addAllin interfaceCollection<V>- Specified by:
addAllin interfaceList<V>- Overrides:
addAllin classAbstractCollection<V>- Parameters:
c- the elements to be inserted into this list.- Returns:
- true if some elements has been added.
-
clear
public void clear()Removes all of the elements from this list. The list will be empty after this call returns.- Specified by:
clearin interfaceCollection<V>- Specified by:
clearin interfaceList<V>- Overrides:
clearin classAbstractList<V>
-
clearNoGC
public void clearNoGC()same as clear(), but references to objects are kept internally. This allows the operation to be constant time, but implies that objects will not be garbage collected until their references are overwritten to store other objects. -
contains
Returnstrueif this list contains the specified element.- Specified by:
containsin interfaceCollection<V>- Specified by:
containsin interfaceList<V>- Overrides:
containsin classAbstractCollection<V>- Parameters:
elem- element whose presence in this list is to be tested.- Returns:
trueif the specified element is present;falseotherwise.
-
ensureCapacity
public void ensureCapacity(int minCapacity) Increases the capacity of thisArrayListinstance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.- Parameters:
minCapacity- the desired minimum capacity.
-
fastRemove
private void fastRemove(int index) Private remove method that skips bounds checking and does not return the value removed. -
get
Returns the element at the specified position in this list. -
pop
It removes and returns the last element in the list.- Returns:
- the last element in the list.
-
push
It inserts the element at the end of the list- Parameters:
element- the added element.
-
indexOf
Searches for the first occurrence of the given argument, testing for equality using theequalsmethod. -
indexOf
Searches for the first occurrence of the given argument, testing for equality using the == method.- Parameters:
elem- an object.lastPosition- last index to which it should check.- Returns:
- the index of the first occurrence of the argument in this list; returns value -1 if the object is not found.
-
isEmpty
public boolean isEmpty()Tests if this list has no elements.- Specified by:
isEmptyin interfaceCollection<V>- Specified by:
isEmptyin interfaceList<V>- Overrides:
isEmptyin classAbstractCollection<V>- Returns:
- true if this list has no elements; false otherwise.
-
lastIndexOf
Returns the index of the last occurrence of the specified object in this list.- Specified by:
lastIndexOfin interfaceList<V>- Overrides:
lastIndexOfin classAbstractList<V>- Parameters:
elem- the desired element.- Returns:
- the index of the last occurrence of the specified object in this list; returns -1 if the object is not found.
-
remove
Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices). -
remove
Removes a single instance of the specified element from this list, if it is present (optional operation). More formally, removes an elementesuch that(o==null ? e==null : o.equals(e)), if the list contains one or more such elements. Returnstrueif the list contained the specified element (or equivalently, if the list changed as a result of the call).- Specified by:
removein interfaceCollection<V>- Specified by:
removein interfaceList<V>- Overrides:
removein classAbstractCollection<V>- Parameters:
o- element to be removed from this list, if present.- Returns:
- true if the list contained the specified element.
-
set
Replaces the element at the specified position in this list with the specified element. -
setElementAt
Replaces the element at the specified position in this list with the specified element.- Parameters:
element- element to be stored at the specified position.index- index of element to replace.
-
size
public int size()Returns the number of elements in this list.- Specified by:
sizein interfaceCollection<V>- Specified by:
sizein interfaceList<V>- Specified by:
sizein classAbstractCollection<V>- Returns:
- the number of elements in this list.
-
toArray
Returns an array containing all of the elements in this list in the correct order.- Specified by:
toArrayin interfaceCollection<V>- Specified by:
toArrayin interfaceList<V>- Overrides:
toArrayin classAbstractCollection<V>- Returns:
- an array containing all of the elements in this list in the correct order.
-
toArray
public <T> T[] toArray(T[] a) Returns an array containing all of the elements in this list in the correct order; the runtime type of the returned array is that of the specified array. If the list fits in the specified array, it is returned therein. Otherwise, a new array is allocated with the runtime type of the specified array and the size of this list.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 anynullelements.- Specified by:
toArrayin interfaceCollection<V>- Specified by:
toArrayin interfaceList<V>- Overrides:
toArrayin classAbstractCollection<V>- Type Parameters:
T- the type which is being stored by a SimpleArrayList.- Parameters:
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.- Returns:
- an array containing the elements of the list.
- Throws:
ArrayStoreException- if the runtime type of a is not a supertype of the runtime type of every element in this list.
-
toString
Check if the given index is in range. If not, throw an appropriate runtime exception. This method does *not* check if the index is negative: It is always used immediately prior to an array access, which throws an ArrayIndexOutOfBoundsException if index is negative.- Overrides:
toStringin classAbstractCollection<V>
-
trimToSize
public void trimToSize()Trims the capacity of thisArrayListinstance to be the list's current size. An application can use this operation to minimize the storage of anArrayListinstance.
-