An array is a list where elements are stored contiguously. Elements of the array are indexed from zero and indices must be of type Int
. It can be built with elements between [
and ]
.
Static Methods
Methods
append(item)
Add item
at the end of the array. Return item
.
def a = []
a.append(1)
a.append('a')
IO.println(a) #> [1, a]
append_all(other)
Add all the elements of other
at the end of the array. other
must be iterable. Return other
.
def a = [ 1, 'a' ]
a.append_all([ 3.14, "good", true ])
IO.println(a) #> [1, a, 3.14, good, true]
at(index)
Get the element at index
. index
can be negative to indicate a position from the end, -1
being the last element. index
must be valid.
def a = [ 1, 'a', 3.14, "good", true ]
IO.println(a.at(3)) #> good
IO.println(a.at(-1)) #> true
See also: [ index ] operator
clear()
Remove all the elements from the array.
def a = [ 1, 'a', 3.14, "good", true ]
a.clear()
IO.println(a) #> []
clone()
Get a deep copy of the array. The elements of the array must implement the clone
method.
def a0 = [ 1, 'a', 3.14, "good", true ]
def a1 = a0.clone()
IO.println(a1) #> [1, a, 3.14, good, true]
erase(index)
Remove the element at index
. index
can be negative to indicate a position from the end, -1
being the last element. All the subsequent elements are shifted one position to the left. Return the removed element.
def a = [ 1, 'a', 3.14, "good", true ]
a.erase(1)
IO.println(a) #> [1, 3.14, good, true]
a.erase(-1)
IO.println(a) #> [1, 3.14, good]
find(item)
Return the index of the first element equals to item
in the array, or -1
if item
is not found in the array.
def a = [ 1, 'a', 3.14, "good", true ]
IO.println(a.find("good")) #> 3
IO.println(a.find(nil)) #> -1
insert(index, item)
Insert item
at index
in the array. index
can be equal to the number of elements to append item
at the end of the list. index
can be negative to indicate a position from the end, -1
being the last position after item
is inserted. Elements after index
are shifted one position to the right. Return item
.
def a = [ 1, 3.14 ]
a.insert(1, 'a')
IO.println(a) #> [1, a, 3.14]
a.insert(3, "good")
IO.println(a) #> [1, a, 3.14, good]
a.insert(-1, true)
IO.println(a) #> [1, a, 3.14, good, true]
iterate(iterator), iterator_value(iterator)
Implement the iterator protocol to iterate over the elements of the array.
partition(pred)
Partition the array in two, with all the elements for which the predicate pred
is true preceding all the elements for which the predicate pred
is false. Return the index of the first element in the second group (which may be the number of elements in the array if no element is in the second group).
def a = [ 5, 10, 3, 6, 9, 2, 7 ]
IO.println(a.partition() {|item| item % 2 == 0 }) #> 3
remove(item)
Remove the first element equals to item
from the array. All the subsequent elements are shifted one position to the left. Return the removed element if found, or nil
if not found.
def a = [ 1, true, 'a', 3.14, "good", true ]
a.remove(true)
IO.println(a) #> [1, a, 3.14, good, true]
IO.println(a.remove("bad")) #> nil
reverse()
Reverse the order of the elements of the array.
IO.println([].reverse()) #> []
IO.println([1, true].reverse()) #> [true, 1]
IO.println([1, true, 3.14].reverse()) #> [3.14, true, 1]
size
Return the number of elements in the array.
def a = [ 1, 'a', 3.14, "good", true ]
IO.println(a.size) #> 5
sort(), sort(comp)
Sort the array in place. Comparator comp
is used to compare two elements, if it returns true
then the first element will be put before the second element in the sorted array. If comp
is not provided, <
is used to compare elements.
def a = [ 5, 10, 3, 6, 9, 2, 7 ]
a.sort()
IO.println(a) #> [2, 3, 5, 6, 7, 9, 10]
a.sort {|lhs,rhs| lhs > rhs }
IO.println(a) #> [10, 9, 7, 6, 5, 3, 2]
swap(index0,index1)
Swap the elements at index0
and index1
in the array.
def a = [ 1, 'a', 3.14, "good", true ]
a.swap(0, 3)
IO.println(a) #> [good, a, 3.14, 1, true]
to_a
Transform the array to a new array with the same elements.
def a = [ 1, 'a', 3.14, "good", true ]
def b = a.to_a
IO.println(b) #> [1, a, 3.14, good, true]
IO.println(Object.same(a, b)) #> false
to_s
Transform the array to a String
.
def a = [ 1, 'a', 3.14, "good", true ]
IO.println(a.to_s) #> [1, a, 3.14, good, true]
Operators
[ index ] operator
Get the element at index
. index
can be negative to indicate a position from the end, -1
being the last element. If index
is not valid, nil
is returned.
index
can also be a Range
. The result is an array with the elements of the array in the range. In this case, the range must be valid.
def a = [ 1, 'a', 3.14, "good", true ]
IO.println(a[3]) #> good
IO.println(a[-1]) #> true
IO.println(a[0..1]) #> [1, a]
See also: [ index ]=(item) operator, at(index)
[ index ]=(item) operator
Set the element at index
to item
. index
can be negative to indicate a position from the end, -1
being the last element. Return item
.
def a = [ 1, 'a', 3.14, "good", true ]
a[1] = nil
IO.println(a) #> [1, nil, 3.14, good, true]
See also: [ index ] operator
+(other)
Concatenate the array with other
. other
must be iterable.
def a = [ 1, 'a', 3.14 ]
def b = a + [ "good", true ]
IO.println(b) #> [1, a, 3.14, good, true]