# ADT:ArrayList

### Import List

        ADT:Comparator
        ADT:Storable
        IO
        Object
        Object
        RT0

Class List
ArrayListDesc | Resizable-array implementation of a list.

Class Summary: ArrayListDesc [Detail]

      +---RT0.Object
           |
           +---Object.Object
                |
                +---ADT:Storable.Object
                     |
                     +--ADT:ArrayList.ArrayListDesc

Resizable-array implementation of a list.

Field Summary
array-: Array E Holds the elements of the list.
size-: LONGINT Number of elements in the array.
Inherited Methods
From RT0.Object: Finalize From Object.Object: Equals, HashCode, ToString From ADT:Storable.Object: Load, Store

Type Summary
[Array] = POINTER TO ARRAY OF E
ArrayList = POINTER TO ArrayListDesc E
IterElements = POINTER TO IterElementsDesc E
[IterElementsDesc] = RECORD ... END

Procedure Summary
Append(E) Appends the specified element obj to the end of this list.
Clear() Removes all of the elements from this list.
Contains(E): BOOLEAN Returns TRUE if this list contains the specified element.
Copy(): ArrayList E Returns a shallow copy of l.
[CreateArray](LONGINT, ArrayList E)
Destroy()
Elements(): ObjectArrayPtr E
EnsureCapacity(LONGINT) Increases the capacity of l, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument size.
Equals(Object): BOOLEAN Indicates whether some other object is "equal to" this one.
Get(LONGINT): E Returns the element at the specified position index in this list.
HashCode(): Hash Returns a hash code value for the object.
INIT(LONGINT) Initializes a list with the stated initial size.
INIT(ArrayList E)
IndexOf(E): LONGINT Searches for the first occurence of the given argument, testing for equality using the Object.Object.Equals method.
Insert(LONGINT, E) Inserts the specified element obj at the specified position index in this list.
IsEmpty(): BOOLEAN Tests if this list has no elements.
IterElements(): IterElements E Returns an iterator that traverses the elements of l sequentially.
LastIndexOf(E): LONGINT Searches for the last occurence of the given argument, testing for equality using the Object.Object.Equals method.
Load(Reader) Loads data of l from r.
New(LONGINT): ArrayList Creates a new list with the stated initial size.
Next(VAR E): BOOLEAN
Remove(LONGINT) Removes the element at the specified position index in this list.
RemoveRange(LONGINT, LONGINT) Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive.
Set(LONGINT, E) Replaces the element at the specified position index in this list with the specified element obj.
Size(): LONGINT Returns the number of elements in this list.
Sort(LONGINT, LONGINT, Comparator) Sort array interval ``a.array[fromIndex, toIndex[`' according to the comparator function cmp.
Store(Writer) Stores data of l to w.
TrimToSize() Trims the capacity of l to be the list's current size.

Class Detail: ArrayListDesc

Field Detail

### array

    FIELD array-: ArrayE

> Holds the elements of the list. The index values in ``[0..size-1]`' refer to valid entries.

### size

    FIELD size-: LONGINT

> Number of elements in the array. This value is always less or equal to ``LEN(array)`'.

Type Detail

### Array

    TYPE [Array] = POINTER TO ARRAY OF E

### ArrayList

    TYPE ArrayList = POINTER TO ArrayListDescE

### IterElements

    TYPE IterElements = POINTER TO IterElementsDescE

### IterElementsDesc

    TYPE [IterElementsDesc] = RECORD
                            END

Procedure Detail

### Append

    PROCEDURE (l: ArrayList) Append(obj: E)

> Appends the specified element obj to the end of this list.

### Clear

    PROCEDURE (l: ArrayList) Clear()

> Removes all of the elements from this list. The list will be empty after this call returns.

### Contains

    PROCEDURE (l: ArrayList) Contains(obj: E): BOOLEAN

> Returns TRUE if this list contains the specified element.

### Copy

    PROCEDURE (l: ArrayList) Copy(): ArrayListE

> Returns a shallow copy of l. The elements themselves are not copied.

### CreateArray

    PROCEDURE (l: ArrayList) [CreateArray](size: LONGINT;
                          old: ArrayListE)

### Destroy

    PROCEDURE (l: ArrayList) Destroy()

### Elements

    PROCEDURE (l: ArrayList) Elements(): ObjectArrayPtrE

### EnsureCapacity

    PROCEDURE (l: ArrayList) EnsureCapacity(size: LONGINT)

> Increases the capacity of l, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument size.

> Pre-condition: ``size >= 0`'

### Equals

    PROCEDURE (l: ArrayList) Equals(obj: Object): BOOLEAN

> Indicates whether some other object is "equal to" this one.

> The Object.Equals method implements an equivalence relation:

> - It is reflexive: for any reference value ``x`', ``x.Equals(x)`' should return TRUE.

> - It is symmetric: for any reference values ``x`' and ``y`', ``x.Equals(y)`' should return TRUE if and only if ``y.Equals(x)`' returns TRUE.

> - It is transitive: for any reference values ``x`', ``y`', and ``z`', if ``x.Equals(y)`' returns TRUE and ``y.Equals(z)`' returns TRUE, then ``x.Equals(z)`' should return TRUE.

> - It is consistent: for any reference values ``x`' and ``y`', multiple invocations of ``x.Equals(y)`' consistently return TRUE or consistently return FALSE, provided no information used in equals comparisons on the object is modified.

> - For any non-NIL reference value ``x`', ``x.Equals(NIL)`' should return FALSE.

> The ``Equals`' method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any reference values ``x`' and ``y`', this method returns TRUE if and only if ``x`' and ``y`' refer to the same object (``x=y`' has the value TRUE).

> [Description inherited from Equals]

### Get

    PROCEDURE (l: ArrayList) Get(index: LONGINT): E

> Returns the element at the specified position index in this list.

> Pre-condition: ``0 <= index`' and ``index < l.Size()`'

### HashCode

    PROCEDURE (l: ArrayList) HashCode(): Hash

> Returns a hash code value for the object. This method is supported for the benefit of dictionaries such as those provided by ADT:Dictionary.

> The general contract of Object.HashCode is:

> - Whenever it is invoked on the same object more than once during an execution of an application, the Object.HashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.

> - If two objects are equal according to the Object.Equals method, then calling the Object.HashCode method on each of the two objects must produce the same integer result.

> - It is not required that if two objects are unequal according to the Object.Equals method, then calling the Object.HashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of dictionaries.

> As much as is reasonably practical, the Object.HashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required.)

> [Description inherited from HashCode]

### INIT

    PROCEDURE (l: ArrayList) INIT(initialSize: LONGINT)

> Initializes a list with the stated initial size.

### INIT

    PROCEDURE (iter: IterElements) INIT(list: ArrayListE)

### IndexOf

    PROCEDURE (l: ArrayList) IndexOf(obj: E): LONGINT

> Searches for the first occurence of the given argument, testing for equality using the Object.Object.Equals method. Returns -1 if the object is not found.

### Insert

    PROCEDURE (l: ArrayList) Insert(index: LONGINT;
                     obj: E)

> Inserts the specified element obj at the specified position index in this list. Shifts the element currently at that position (if any) and any subsequent elements to the right (adds one to their indices).

> Pre-condition: ``0 <= index`' and ``index <= l.Size()`'

### IsEmpty

    PROCEDURE (l: ArrayList) IsEmpty(): BOOLEAN

> Tests if this list has no elements.

### IterElements

    PROCEDURE (l: ArrayList) IterElements(): IterElementsE

> Returns an iterator that traverses the elements of l sequentially. It produces the elements at indices 0, 1, 2, and so on, until index l.size is reached.

> Behaviour of the iterator is undefined if the array is changed while the iterator is traversing it.

### LastIndexOf

    PROCEDURE (l: ArrayList) LastIndexOf(obj: E): LONGINT

> Searches for the last occurence of the given argument, testing for equality using the Object.Object.Equals method. Returns -1 if the object is not found.

### Load

    PROCEDURE (l: ArrayList) Load(r: Reader)
      RAISES Error;

> Loads data of l from r. Nested record pointers are loaded by calling the type-bound procecdure Reader.ReadObject. This procedure must be symmetric to Object.Store, or data internalization will break, causing undefined object state or program termination.

> Note: When internalizing a file with alien objects, it is possible that the type-bound procedure Object.Load is invoked more than once for a single object. Except for the results of the last call, all duplicates are discarded. Because of this, all changes by this procedure to any program state that is not part of the object l are deprecated.

> Pre-condition: This procedure is either activated by a super call, or from the procedure Reader.ReadObject.

> [Description inherited from Load]

### New

    PROCEDURE New(initialSize: LONGINT): ArrayList

> Creates a new list with the stated initial size.

> Pre-condition: ``initialSize >= 0`'

### Next

    PROCEDURE (iter: IterElements) Next(VAR elem: E): BOOLEAN

### Remove

    PROCEDURE (l: ArrayList) Remove(index: LONGINT)

> Removes the element at the specified position index in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

> Pre-condition: ``0 <= index`' and ``index < l.Size()`'

### RemoveRange

    PROCEDURE (l: ArrayList) RemoveRange(fromIndex: LONGINT;
                          toIndex: LONGINT)

> Removes from this List all of the elements whose index is between fromIndex, inclusive and toIndex, exclusive. Shifts any succeeding elements to the left (reduces their index). This call shortens the list by ``toIndex - fromIndex`' elements. (If ``toIndex = fromIndex`', this operation has no effect.)

> - ``0 <= fromIndex`' and ``fromIndex < l.Size()`'

> - ``0 <= toIndex`' and ``toIndex < l.Size()`'

> - ``toIndex-fromIndex >= 0`'

### Set

    PROCEDURE (l: ArrayList) Set(index: LONGINT;
                  obj: E)

> Replaces the element at the specified position index in this list with the specified element obj.

> Pre-condition: ``0 <= index`' and ``index < l.Size()`'

### Size

    PROCEDURE (l: ArrayList) Size(): LONGINT

> Returns the number of elements in this list.

### Sort

    PROCEDURE (a: ArrayList) Sort(fromIndex: LONGINT;
                   toIndex: LONGINT;
                   cmp: Comparator)

> Sort array interval ``a.array[fromIndex, toIndex[`' according to the comparator function cmp.

### Store

    PROCEDURE (l: ArrayList) Store(w: Writer)
      RAISES Error;

> Stores data of l to w. Nested record pointers are stored by calling the type-bound procedure Writer.WriteObject. The procedure is not allowed to make any changes to the global state of the program, except for calling the ``Write`' methods of the writer w. Any redefinition of this procedure must include a super call, preferably as the first statement of the procedure body.

> Pre-condition: This procedure is either activated by a super call, or from the procedure Writer.WriteObject.

> [Description inherited from Store]

### TrimToSize

    PROCEDURE (l: ArrayList) TrimToSize()

> Trims the capacity of l to be the list's current size. An application can use this operation to minimize the storage of an ArrayList instance.