ArrayUtils
ArrayUtils provides a dynamic array data structure that can hold elements of any type. It behaves like a typed list with methods for adding, reading, updating, and removing elements.
Create an Array
Use ArrayUtils.empty(<T>) to create a new empty array of a given type.
var names = ArrayUtils.empty(String);
var numbers = ArrayUtils.empty(Integer);
Methods
ArrayUtils.empty(T: type): Array<T>
Creates and returns a new empty array of the specified type.
@param type — The type of elements the array will hold (e.g. String, Integer, File).
@returns — A new empty Array<T>.
add(value: T): void
Appends a value to the end of the array.
@param value — The value to add.
names.add("Alice");
names.add("Bob");
get(index: Integer): T
Returns the element at the specified index.
@param index — Zero-based index of the element to retrieve.
@returns — The element at the given index.
var first = names.get(0); // "Alice"
set(index: Integer, value: T): void
Replaces the element at the specified index with a new value.
@param index — Zero-based index of the element to replace.
@param value — The new value.
names.set(0, "Carol"); // replaces "Alice" with "Carol"
remove(index: Integer): void
Removes the element at the specified index.
@param index — Zero-based index of the element to remove.
names.remove(1); // removes "Bob"
size(): Integer
Returns the number of elements in the array.
@returns — The current size of the array.
var count = names.size();
clear(): void
Removes all elements from the array.
names.clear();
Full Example
var array = ArrayUtils.empty(String);
array.add("N");
array.add("B");
array.add("C");
if (array.get(0) == "N") {
array.set(0, "A");
}
array.add("D");
array.remove(3);
array.clear();
return array.size() == 0; // true