Libraries.Containers.Array Documentation
The Array class is a data structure that stores items in contiguous memory. An item is typically stored and accessed through an index or location. This location always starts at 0, this means the first item in the array is at location 0, the second is at location 1, etc. The default maximum size is set to 10, but can be changed by using the SetSize(value) method or the array will automatically make itself large when the space is needed (note: it is possible to turn the resizing off with the SetAutoResize(false) method).
Example Code
use Libraries.Containers.Array
class Main
action Main
//make the array
Array<integer> myArray
//add a value
myArray:Add(12)
//get it back
integer value = myArray:Get(0)
end
end
Inherits from: Libraries.Language.Object
Summary
Actions Summary Table
Actions | Description |
---|---|
Add(integer location, Type value) | This action adds a value at a location in the indexed object. |
Add(Type value) | This action adds a value to the end of the array. |
AddToEnd(Type value) | This action adds an item to the end of the array. |
AddToFront(Type value) | This action adds an item to the front of the array at index 0. |
Compare(Libraries.Language.Object object) | This action compares two object hash codes and returns a CompareResult. |
Copy() | This action returns a deep copy of the array in question. |
CopyToArray() | This action copies the list to an array data structure. |
Empty() | This action empty's the list, clearing out all of the items contained within it. |
Equals(Libraries.Language.Object object) | This action determines if two objects are equal based on their hash code values. |
Get(integer location) | This action gets the item at a given location in an array. |
GetAutoResize() | This action returns true if the array is dynamic(resizable) or false if the array does not automatically resize. |
GetFirstLocation(Type value) | This action gets the first occurrence of the item and returns its location. |
GetFromEnd() | This action gets the item at the end of the array(the item will remain in the array). |
GetFromFront() | This action gets the item at the front of the array(the item will remain in the array). |
GetHashCode() | This action gets the hash code for an object. |
GetIterator() | This action gets an iterator for the object and returns that iterator. |
GetLastLocation(Type value) | This action gets the last occurrence of the item and returns its location. |
GetMaxSize() | This action gets the number of items that can be stored in the array(max size). |
GetSize() | This action gets the size of the array. |
Has(Type value) | This action determines if an addable object contains a certain item. |
IsEmpty() | This action returns a boolean value, true if the container is empty and false if it contains any items. |
Remove(Type value) | This action removes the first occurrence of an item that is found in the Addable object. |
RemoveAll(Type value) | This action removes all occurrences of an item from the Addable object. |
RemoveAt(integer location) | This action removes an item from an indexed object and returns that item. |
RemoveFromEnd() | This action removes the item at the end of the array. |
RemoveFromFront() | This action removes the item at the front of the list. |
Set(integer location, Type value) | This action sets the item at a given location in the indexed object to a new item. |
SetAutoResize(boolean resizable) | This action changes the flag that tells the structure if it is a dynamic array or not. |
SetMaxSize(integer size) | This action sets the number of items that can be stored in the array(max size). |
SetSize(integer size) | This action sets the size of the array and fills it with undefined items. |
Sort(Libraries.Containers.Support.Comparison comparison) | This action sorts the values of the array using a merge sort algorithm. |
Sort() | This action sorts the values of the array using a merge sort algorithm. |
Actions Documentation
Add(integer location, Type value)
This action adds a value at a location in the indexed object.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:Add(0, 22)
Parameters
- integer location: The index or location the value will be stored at.
- Libraries.Language.Object: The item to be added to the indexed object.
Add(Type value)
This action adds a value to the end of the array.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:Add(22)
Parameters
- Libraries.Language.Object: The value to be inserted.
AddToEnd(Type value)
This action adds an item to the end of the array. If the max size has been reached an the array is not re-sizable an InvalidLocationError will be alerted.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:AddToEnd(12)
Parameters
- Libraries.Language.Object: The item to be added to the array.
AddToFront(Type value)
This action adds an item to the front of the array at index 0. Then moves all other items down one index. If the max size is already reached and the array is not re-sizable an InvalidLocationError will be alerted.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:AddToFront(12)
Parameters
- Libraries.Language.Object: The item to be added to the array.
Compare(Libraries.Language.Object object)
This action compares two object hash codes and returns a CompareResult. The compare result is either larger if this hash code is larger than the object passed as a parameter, smaller, or equal.
Example Code
use Libraries.Language.Support.CompareResult
Object o
Object t
CompareResult result = o:Compare(t)
Parameters
- Libraries.Language.Object: The object to compare to.
Return
Libraries.Language.Support.CompareResult: The Compare result, Smaller, Equal, or Larger.
Copy()
This action returns a deep copy of the array in question. As such, all elements are copied from one array to the other. While the array is a deep copy, the elements inside of the array are not copied.
Example Code
//the array class is Copyable
use Libraries.Containers.Array
Array<integer> array
Object o = array:Copy()
Array<integer> copy = cast(Array<integer>, o)
Return
Libraries.Language.Object: Returns a deep copy of the array.
CopyToArray()
This action copies the list to an array data structure.
Return
Libraries.Containers.Array: This returns an array of the list.
Empty()
This action empty's the list, clearing out all of the items contained within it.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
myArray:Empty()
Equals(Libraries.Language.Object object)
This action determines if two objects are equal based on their hash code values.
Example Code
use Libraries.Language.Object
use Libraries.Language.Types.Text
Object o
Text t
boolean result = o:Equals(t)
Parameters
- Libraries.Language.Object: The to be compared.
Return
boolean: True if the hash codes are equal and false if they are not equal.
Get(integer location)
This action gets the item at a given location in an array.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
integer result = myArray:Get(0)
Parameters
- integer location: The index or location the value is located at.
Return
Libraries.Language.Object: The item at the given location.
GetAutoResize()
This action returns true if the array is dynamic(resizable) or false if the array does not automatically resize.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
boolean result = myArray:GetAutoResize()
Return
boolean: True if the array is resizable and false if it is not.
GetFirstLocation(Type value)
This action gets the first occurrence of the item and returns its location. If the item was not found -1 is returned.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
integer location = myArray:GetFirstLocation(22)
Parameters
- Libraries.Language.Object: The item being searched for.
Return
integer: The location of the first occurrence of the item.
GetFromEnd()
This action gets the item at the end of the array(the item will remain in the array).
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:Add(4)
myArray:Add(13)
myArray:Add(12)
integer value = myArray:GetFromEnd()
Return
Libraries.Language.Object: The item at the end of the array.
GetFromFront()
This action gets the item at the front of the array(the item will remain in the array).
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:Add(4)
myArray:Add(13)
myArray:Add(12)
integer value = myArray:GetFromFront()
Return
Libraries.Language.Object: The item at the front of the array.
GetHashCode()
This action gets the hash code for an object.
Example Code
Object o
integer hash = o:GetHashCode()
Return
integer: The integer hash code of the object.
GetIterator()
This action gets an iterator for the object and returns that iterator.
Example Code
use Libraries.Containers.Array
use Libraries.Containers.Iterator
Array<integer> myArray
myArray:SetSize(4)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
Iterator<integer> it = myArray:GetIterator()
Return
Libraries.Containers.Iterator: Returns the iterator for an object.
GetLastLocation(Type value)
This action gets the last occurrence of the item and returns its location. If the item was not found -1 is returned.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
integer location = myArray:GetLastLocation(22)
Parameters
- Libraries.Language.Object: The item being searched for.
Return
integer: The location of the last occurrence of the item.
GetMaxSize()
This action gets the number of items that can be stored in the array(max size).
Example Code
use Libraries.Containers.Array
Array<integer> myArray
integer maxSize = myArray:GetMaxSize()
Return
integer:
GetSize()
This action gets the size of the array.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
integer size = myArray:GetSize()
Return
integer:
Has(Type value)
This action determines if an addable object contains a certain item.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
boolean result = myArray:Has(33)
Parameters
- Libraries.Language.Object: The item to find in the Addable object.
Return
boolean: Returns true if the item was in the Addable object and false if it was not.
IsEmpty()
This action returns a boolean value, true if the container is empty and false if it contains any items.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
boolean result = myArray:IsEmpty()
Return
boolean: Returns true when the container is empty and false when it is not.
Remove(Type value)
This action removes the first occurrence of an item that is found in the Addable object.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
boolean result = myArray:Remove(22)
Parameters
- Libraries.Language.Object: The item to find in the Addable object.
Return
boolean: Returns true if the item was removed and false if it was not removed.
RemoveAll(Type value)
This action removes all occurrences of an item from the Addable object.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
boolean result = myArray:RemoveAll(22)
Parameters
- Libraries.Language.Object: The item to find in the Addable object.
Return
boolean: Returns true if the item was removed and false if it was not removed.
RemoveAt(integer location)
This action removes an item from an indexed object and returns that item.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
integer item = myArray:RemoveAt(2)
Parameters
- integer location: The index or location of the item to remove.
Return
Libraries.Language.Object: The item that was removed from the indexed object.
RemoveFromEnd()
This action removes the item at the end of the array.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:Add(33)
myArray:Add(13)
myArray:Add(43)
integer removed = myArray:RemoveFromEnd()
Return
Libraries.Language.Object: The item at the end of the array.
RemoveFromFront()
This action removes the item at the front of the list.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:Add(33)
myArray:Add(13)
myArray:Add(43)
integer removed = myArray:RemoveFromFront()
Return
Libraries.Language.Object: The item at the front of the list.
Set(integer location, Type value)
This action sets the item at a given location in the indexed object to a new item.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
Parameters
- integer location: The index or location the value will be stored at.
- Libraries.Language.Object: The item to be added to the indexed object.
SetAutoResize(boolean resizable)
This action changes the flag that tells the structure if it is a dynamic array or not. If it is dynamic(an array list) then resizable is true and if it is a standard array(not dynamic) then resizable is false.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetAutoResize(false)
myArray:SetSize(10)
Parameters
- boolean resizable: The value to set the resizable flag to.
SetMaxSize(integer size)
This action sets the number of items that can be stored in the array(max size). The max size can only be increased, any value that is lower will leave the array with the same max size it had.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetMaxSize(20)
Parameters
- integer size: The max size to set for the array.
SetSize(integer size)
This action sets the size of the array and fills it with undefined items. Changing the size of the array means any items already in the array will be copied over.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
Parameters
- integer size: The size of the array.
Sort(Libraries.Containers.Support.Comparison comparison)
This action sorts the values of the array using a merge sort algorithm. It is guaranteed to execute in O(n log n). The example uses a support class called IntegerComparison, which duplicates the default sorting for integers.
Example Code
use Libraries.Containers.Support.Comparison
class IntegerComparison is Comparison
action Compare(Object left, Object right) returns integer
Integer l = cast(Integer, left)
Integer r = cast(Integer, right)
if l:GetValue() < r:GetValue()
return parent:Comparison:SMALLER
elseif l:GetValue() > r:GetValue()
return parent:Comparison:LARGER
else
return parent:Comparison:EQUAL
end
end
end
use Libraries.Containers.Array
Array<integer> myArray
IntegerComparison compare
myArray:Add(33)
myArray:Add(13)
myArray:Add(43)
myArray:Sort(compare)
Parameters
Sort()
This action sorts the values of the array using a merge sort algorithm. It is guaranteed to execute in O(n log n).
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:Add(33)
myArray:Add(13)
myArray:Add(43)
myArray:Sort()