#API to join two arrays of same type

1 messages · Page 1 of 1 (latest)

stone swift
#

Hi Team,
Are there any ballerina collection apis to join two arrays of same type?

thorny hollow
#

Please see comments in-line.

import ballerina/io;

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

    // Creates a new list with the members from `x` and `y`.
    int[] arr = [...x, ...y];
    io:println(arr); // [1,2,3,4,5]

    // `x` and `y` don't change.
    io:println(x); // [1,2]
    io:println(y); // [3,4,5]

    // Pushes all of the members in `y` to x
    x.push(...y);
    // `x` changes now.
    io:println(x); // [1,2,3,4,5]
    // `y` remains the same.
    io:println(y); // [3,4,5]
}