Array and Tuple
ballerina/lang.array
Post
Array Lang Library
The Ballerina Array lang library - ballerina/lang.array
provides functions that operate on list values, such as arrays and tuples.
See available operations in the ballerina/lang.boolean module.
|
|
bal run lang_array.bal
length: 6
index: 2
sortedNumbers: [-2,0,1,5,8,9]
bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
💡 array is not a pre-defined module prefix (because array is not a keyword).
To use it with function call syntax, you must have an import ballerina/lang.array
declaration.
However, you can call these functions on a list value using the method call syntax.
Functions
The graphical examples are for illustration purposes only and provide a visual aid to help understand the concept easily. They are not actual syntax.
Stack and Queue Operations
Pop
Removes and returns the last element of the list.
[🍎, 🍇, 🍌, 🍓].pop()
↓
Return = 🍓
List = [🍎, 🍇, 🍌]
The pop
function removes and returns the last member of a given list. The arr argument specifies the list from which to remove the last member. The function returns the removed member.
Push
Adds values to the end of an array.
[🍎, 🍇, 🍌, 🍓].push(🍒, 🍉)
↓
Return = ()
List = [🍎, 🍇, 🍌, 🍓, 🍒, 🍉]
The push
function adds one or more values to the end of a given list. The arr argument specifies the list to which the values will be added, and the vals argument is a variable-length argument that can contain one or more values to add to the list.
Shift
Removes and returns the first member of an array.
[🍎, 🍇, 🍌, 🍓].shift()
↓
Return = 🍎
List = [🍇, 🍌, 🍓]
The shift
function removes and returns the first member of a given list. The arr argument specifies the list from which to remove the first member. The function returns the removed member.
Unshift
Adds values to the start of an array.
[🍎, 🍇, 🍌, 🍓].unshift(🍒, 🍉)
↓
Return = ()
List = [🍒, 🍉, 🍎, 🍇, 🍌, 🍓]
The unshift
function adds one or more values to the start of a given list. The arr argument specifies the list to which the values will be added, and the vals argument is a variable-length argument that can contain one or more values to add to the list.
|
|
bal run lang_array_stack_queue.bal
Numbers: [1,2,3,4,5]
Numbers After Push: [1,2,3,4,5,6,7]
Numbers After Pop: [1,2,3,4,5,6]
Last: 7
Numbers After Shift: [2,3,4,5,6]
First: 1
Numbers After Unshift: [-1,0,2,3,4,5,6]
bal version
Ballerina 2201.6.0 (Swan Lake Update 6)