Class ArrayList
An array-based List of items.
A List implementation that stores items using an array. This provides random access to any index, however does so at the expense of insertion and deletion everywhere except at the end.
Access, insertion at the end, and deletion at the end are Θ(1). Insertion elsewhere, deletion elsewhere, and search are Θ(n).
ArrayList implements all optional List and Collection methods.
Extends: AbstractList
Implements: List, Collection, Enumerable
Functions
| ArrayList.new(collection) | Creates a new ArrayList. |
Methods
| ArrayList:Add(item) | Adds an item to the ArrayList. |
| ArrayList:Clear() | Removes everything from the ArrayList. |
| ArrayList:Count() | Gets the number of items in the ArrayList. |
| ArrayList:Delete(index) | Removes the item at the specified index from the ArrayList. |
| ArrayList:Enumerator() | Creates an enumerator for the ArrayList. |
| ArrayList:First() | Gets the item at the beginning of the ArrayList. |
| ArrayList:Get(index) | Gets the item at the specified index in the ArrayList. |
| ArrayList:IndexOf(item, index) | Determines the index of a specific item in the ArrayList. |
| ArrayList:Insert(index, item) | Inserts the item into the ArrayList at the specified index. |
| ArrayList:InsertAll(index, items) | Inserts multiple items into the ArrayList at the specified index. |
| ArrayList:Last() | Gets the item at the end of the ArrayList. |
| ArrayList:LastIndexOf(item) | Determines the last index of a specific item in the ArrayList. |
| ArrayList:Pop() | Gets an item from the end and removes that item from the ArrayList. |
| ArrayList:Push(item) | Adds an item to the end of the ArrayList. |
| ArrayList:Remove(item) | Removes the specified item from the ArrayList. |
| ArrayList:RemoveAll(items) | Removes all provided items from the ArrayList. |
| ArrayList:RetainAll(items) | Removes all items except those provided from the ArrayList. |
| ArrayList:Set(index, item) | Sets the element at the specified index. |
| ArrayList:Shift() | Gets an item from the beginning and removes that item from the ArrayList. |
| ArrayList:Sub(first, last) | Creates a new sub-list of this ArrayList. |
| ArrayList:Unshift(item) | Adds an item to the beginning of the ArrayList. |
Functions
Methods- ArrayList.new(collection)
-
Creates a new ArrayList.
Creates a ArrayList copy of the provided Collection or array-indexed
table if one is provided, otherwise creates an empty ArrayList.
Parameters:
- collection the Collection or table to copy
Returns:
-
the new ArrayList
Methods
- ArrayList:Add(item)
-
Adds an item to the ArrayList.
Inherited from:
Parameters:
- item the item to add
Returns:
-
true always since the ArrayList is always changed
- ArrayList:Clear()
-
Removes everything from the ArrayList.
Inherited from:
- ArrayList:Count()
-
Gets the number of items in the ArrayList.
Inherited from:
Returns:
-
the number of items
- ArrayList:Delete(index)
-
Removes the item at the specified index from the ArrayList.
Shifts other elements to fill the gap left at the index of removal.
Inherited from:
Parameters:
- index the index of the item to remove from the ArrayList
Returns:
-
true always since the ArrayList is always changed
Raises:
if the index is out of bounds of the ArrayList - ArrayList:Enumerator()
-
Creates an enumerator for the ArrayList.
The enumerator can be used directly in a generic for loop similar to pairs
or ipairs.
Inherited from:
Returns:
- the enumerator generator
- the invariant state
- the control variable state
- ArrayList:First()
-
Gets the item at the beginning of the ArrayList.
Inherited from:
Returns:
-
the first item in the ArrayList
Raises:
if the ArrayList is empty - ArrayList:Get(index)
-
Gets the item at the specified index in the ArrayList.
Inherited from:
Parameters:
- index the index to get
Returns:
-
the item in the ArrayList at the specified index
Raises:
if the index is out of bounds of the ArrayList - ArrayList:IndexOf(item, index)
-
Determines the index of a specific item in the ArrayList.
Starts from a specified index or from the beginning if none is provided.
Inherited from:
Parameters:
- item the item to locate
- index the index to start looking from
Returns:
-
the index of the item in the ArrayList if found, 0 otherwise
Raises:
if the index is out of bounds of the ArrayList - ArrayList:Insert(index, item)
-
Inserts the item into the ArrayList at the specified index.
Shifts other elements to make space at the index of insertion.
Inherited from:
Parameters:
- index the index to insert the item in the ArrayList
- item the item to add
Returns:
-
true always since the ArrayList is always changed
Raises:
if the index is out of bounds of the ArrayList - ArrayList:InsertAll(index, items)
-
Inserts multiple items into the ArrayList at the specified index.
Inserts all items from the provided Collection in an arbitrary,
deterministic order. The order is the same as the order of enumeration.
Shifts other elements to make space at the index of insertion.
Inherited from:
Parameters:
- index the index to insert the items in the ArrayList
- items the Collection of items to add to this ArrayList
Returns:
-
true always since the ArrayList is always changed
Raises:
if the index is out of bounds of the ArrayList - ArrayList:Last()
-
Gets the item at the end of the ArrayList.
Inherited from:
Returns:
-
the last item in the ArrayList
Raises:
if the ArrayList is empty - ArrayList:LastIndexOf(item)
-
Determines the last index of a specific item in the ArrayList.
Only returns the very last occurrence of the item in the ArrayList.
Inherited from:
Parameters:
- item the item to locate
Returns:
-
the index of the item in the ArrayList if found, 0 otherwise
- ArrayList:Pop()
-
Gets an item from the end and removes that item from the ArrayList.
Inherited from:
Returns:
-
the item in the ArrayList
Raises:
if the ArrayList is empty - ArrayList:Push(item)
-
Adds an item to the end of the ArrayList.
Inherited from:
Parameters:
- item the item to add
Returns:
-
true always since the ArrayList is always changed
- ArrayList:Remove(item)
-
Removes the specified item from the ArrayList.
Removes only a single item. If there are multiple of the same item, it
removes only the first encountered.
When an item is removed any others are shifted to fill the gap left at the index of removal.
Inherited from:
Parameters:
- item the item to remove from the ArrayList
Returns:
-
true if the ArrayList changed as a result, false otherwise
- ArrayList:RemoveAll(items)
-
Removes all provided items from the ArrayList.
Removes each instance of a provided item only once for each time provided.
If there are multiple of the same item in this ArrayList, it removes only
the first encountered for each provided.
Inherited from:
Parameters:
- items the Collection of items to remove from this ArrayList
Returns:
-
true if the ArrayList changed as a result, false otherwise
- ArrayList:RetainAll(items)
-
Removes all items except those provided from the ArrayList.
Retains only the items contained in the specified Collection. If there are
duplicates they are all kept.
Inherited from:
Parameters:
- items the Collection of items to retain in this ArrayList
Returns:
-
true if the ArrayList changed as a result, false otherwise
- ArrayList:Set(index, item)
-
Sets the element at the specified index.
Inherited from:
Parameters:
- index the index to set
- item the item to set at the index
Returns:
-
true if the ArrayList changed as a result, false otherwise
Raises:
if the index is out of bounds of the ArrayList - ArrayList:Shift()
-
Gets an item from the beginning and removes that item from the ArrayList.
Shifts other elements to fill the gap left.
Inherited from:
Returns:
-
the item in the ArrayList
Raises:
if the ArrayList is empty - ArrayList:Sub(first, last)
-
Creates a new sub-list of this ArrayList.
Creates the list that is the portion of this ArrayList between the
specified indices or from the first specified index to the end if only one
index is specified.
Inherited from:
Parameters:
- first the index to start at
- last the index to end at
Returns:
-
the new ArrayList
Raises:
- if the first index is out of bounds
- if the last index is out of bounds
- if the last index is smaller than the first index
- ArrayList:Unshift(item)
-
Adds an item to the beginning of the ArrayList.
Shifts other elements to make space.
Inherited from:
Parameters:
- item the item to add
Returns:
-
true always since the ArrayList is always changed