ArrayAndListOperations - friendlyhj/ZenUtils GitHub Wiki
@since 1.24.0
Zenscript has two collection types, array and list, declared by Type[]
and [Type]
respectively. The difference of them is array has a fixed length, while the length of list is mutable. The array and list of zs only provide add, remove, indexGet, indexSet, contains, getLength operations. That's not enough. Zenutils add more operations to them.
About addition and removal operations that changes the length, array returns a new array, while list modifies and returns itself.
val array = [1, 2, 3] as int[];
val list = [1, 2, 3] as [int];
val newArray = array.add(4);
val listItself = list.add(4);
print(toString(array)); // [1, 2, 3], addition of array doesn't modify itself
print(toString(list)); // [1, 2, 3, 4], add 4 to the list
print(toString(newArray)); // [1, 2, 3, 4], a new array that copies the origin array and adds 4 to the end
arrayOrList[start .. end]
to slice the array or list from startIndex (include) from endIndex (exclude). For array, the slice is cloned from the original one. But list doesn't clone, the slice shares the same memory. It means the changes from the slice of the list will reflect the original one, and vice-versa.
val list = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] as [int];
list[2 .. 6].reverse();
print(toString(list)); // [0, 1, 5, 4, 3, 2, 6, 7, 8, 9]
Almost operations of array and list are the same, differences will be emphasized.
// Array: E[]
// List: [E]
// The element of array or list: E
// Varargs: E...
// Varargs is an array, but `[]` can be omitted in method call
// array and list can be cast to each other
zenClass E[] {
// Copies the array and adds the given element at the end of the new array.
// Returns a new array
function add(element as E) as E[];
// Returns a new array contains all of the element of the array followed by all of the given elements.
function addAll(elements as E...) as E[];
// *Shallow* clones the array. The objects in the array are not cloned, thus there is no special handling for multi-dimensional arrays.
function clone() as E[];
// Finds the first index of the given object in the array.
// If not found, returns -1
function indexOf(element as E) as int;
// Finds the index of the given object in the array starting at the given index.
// If not found, returns -1
// for list, use slice instead
function indexOf(element as E, startIndex as int) as int;
// Checks whether the array is empty (`.length == 0`)
function isEmpty() as bool;
// Checks whether the array is not empty (`.length != 0`)
function isNotEmpty() as bool;
// Checks whether the array is sorted according to natural ordering.
function isSorted() as bool;
// Checks whether the array is sorted according to the provided Comparator.
// The function should return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
// Unlike list, E can't be primitive types
function isSorted(comparator as function(E, E)int) as bool;
// Finds the last index of the given object in the array
// If not found, returns -1
function lastIndexOf(element as E) as int;
// Finds the last index of the given object in the array starting at the given index.
// If not found, returns -1
// For list, use slice instead
function lastIndexOf(element as E, startIndex as int) as int;
// Removes the element at the specified position from the array. All subsequent elements are shifted to the left (subtracts one from their indices).
// Returns a new array with the same elements of the array except the element on the specified position.
// For list, use removeByIndex instead
function remove(index as int) as E[];
// Removes the elements at the specified positions from the specified array. All remaining elements are shifted to the left.
// Returns a new array with the same elements of the array except those at the specified positions.
// For list, use removeByIndex instead
function removeAll(indices as int...) as E[];
// Removes the first occurrence of the specified element from the array.
// Returns a new array with the same elements of the array except the given element
// For list, use remove instead
function removeElement(E as element) as E[];
// Removes the first occurrence of the specified elements from the array.
// Returns a new array with the same elements of the array except the given elements
// for list, use removeAll instead
function removeElements(E... as elements) as E[];
// Removes all the occurrences of the specified elements from the array.
// Returns a new array with the same elements of the array except the given element
// For list, use removeAllOccurrences instead (double r)
function removeAllOccurences(E as element) as E[];
// Reverses the order of the array.
// Returns nothing.
// There is no special handling for multi-dimensional arrays.
function reverse() as void;
// Shifts the order of the array.
// Returns nothing.
function shift(offset as int) as void;
// Sorts the array according to natural ordering
// Returns nothing.
function sort() as void;
// Sorts the array according to the provided Comparator.
// Returns nothing.
// The function should return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
// Unlike list, E can't be primitive types
function sort(comparator as function(E, E)int) as void;
// Shifts the order of the array in specified range
// Returns nothing.
// For list, use slice instead
function shift(offset as int, startIndex as int, endIndex as int) as void;
// Swaps two elements of the array.
// Returns nothing.
function swap(index1 as int, index2 as int) as void;
}
zenClass [E] {
// Adds an element at the end of the list
// Returns the list itself
function add(element as E) as [E];
// Adds elements at the end of the list
// Returns the list itself
function addAll(elements as E...) as [E];
// *Shallow* clones the list. The objects in the list are not cloned, thus there is no special handling for multi-dimensional lists.
function clone() as [E];
// Clears the list
// Returns the list itself
// array doesn't have the operation, assigning an empty array instead.
function clear() as [E];
// Finds the first index of the given object in the list.
// If not found, returns -1
function indexOf(element as E) as int;
// Checks whether the list is empty (`.length == 0`)
function isEmpty() as bool;
// Checks whether the list is not empty (`.length != 0`)
function isNotEmpty() as bool;
// Checks whether the list is sorted according to natural ordering.
function isSorted() as bool;
// Checks whether the list is sorted according to the provided Comparator.
// The function should return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
// Unlike array, E can be primitive types
function isSorted(comparator as function(E, E)int) as bool;
// Finds the last index of the given object in the list.
// If not found, returns -1
function lastIndexOf(element as E) as int;
// Removes the element at the specified positions from the list.
// Returns the list itself
// For array, use removeAll instead
function removeByIndex(indices... as int) as [E];
// Removes the first occurrence of the specified element from the list.
// Returns NOTHING
// For array, use removeElement instead
function remove(E as element) as void;
// Removes the first occurrence of the specified elements from the list.
// Returns the list itself
// for array, use removeElements instead
function removeAll(E... as elements) as [E];
// Removes all the occurrences of the specified elements from the list.
// Returns the list itself
// For array, use removeAllOccurences instead (single r)
function removeAllOccurrences(E as element) as [E];
// Reverses the order of the list.
// Returns the list itself.
// There is no special handling for multi-dimensional lists.
function reverse() as [E];
// Shifts the order of the list.
// Returns the list itself.
function shift(offset as int) as [E];
// Sorts the list according to natural ordering
// Returns the list itself.
function sort() as [E];
// Sorts the list according to the provided Comparator.
// Returns the list itself.
// The function should return a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.
// Unlike array, E can be primitive types
function sort(comparator as function(E, E)int) as [E];
// Swaps two elements of the list.
// Returns the list itself.
function swap(index1 as int, index2 as int) as [E];
}