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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
import ballerina/io;
import ballerina/lang.array;

public function main() {
    int[] numbers = [5, -2, 9, 1, 0, 8];
    int length = numbers.length();
    int? index = array:indexOf(numbers, 9);
    int[] sortedNumbers = array:sort(numbers);

    io:println("length: ", length);
    io:println("index: ", index);
    io:println("sortedNumbers: ", sortedNumbers);
}
length: 6
index: 2
sortedNumbers: [-2,0,1,5,8,9]
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
import ballerina/lang.array;

public function main() {
    int[] numbers = [5, -2, 9, 1, 0, 8];
    int length = numbers.length();
    int? index = array:indexOf(numbers, 9);
    int[] sortedNumbers = array:sort(numbers);
}
// Highlights Only. Change View  to see full code.
import ballerina/lang.array;

int[] numbers = [5, -2, 9, 1, 0, 8];
int length = numbers.length();
int? index = array:indexOf(numbers, 9);

💡 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.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
import ballerina/io;

public function main() {
    int[] numbers = [1, 2, 3, 4, 5];

    io:println("Numbers: ", numbers);
    numbers.push(6, 7);
    io:println("Numbers After Push: ", numbers);

    int last = numbers.pop();
    io:println("Numbers After Pop: ", numbers);
    io:println("Last: ", last);

    int first = numbers.shift();
    io:println("Numbers After Shift: ", numbers);
    io:println("First: ", first);

    numbers.unshift(-1, 0);
    io:println("Numbers After Unshift: ", numbers);
}
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)
// Partial Code. Change View  to see full code.

public function main() {
    int[] numbers = [1, 2, 3, 4, 5];

    numbers.push(6, 7);

    int last = numbers.pop();

    int first = numbers.shift();

    numbers.unshift(-1, 0);
}
// Highlights Only. Change View  to see full code.
int[] numbers = [1, 2, 3, 4, 5];
numbers.push(6, 7);
int last = numbers.pop();
int first = numbers.shift();
numbers.unshift(-1, 0);
Array Lang Library

Navigation

Site Settings

Site Theme

Source Code Density