How to concatenate two arrays?

Jens Mühlenhoff
[indent=4]

init
    x: array of int = {1, 2, 3}
    y: array of int = {4, 5, 6}
    z: array of int = x + y

The above code produces this error message:

concat_arrays.gs:6.23-6.27: error: Incompatible operand
    z: array of int = x + y

The Vala translation doesn't work any better:

int main () {
    int[] x = {1, 2, 3};
    int[] y = {4, 5, 6};
    int[] z = x + y;
    return 0;
}

The error message is:

concat_arrays_v.vala:4.15-4.19: error: Incompatible operand
    int[] z = x + y;

What is the correct way to do this?

Jens Mühlenhoff

Using GLib.Array<T>:

int main () {
    int[] x = {1, 2, 3};
    int[] y = {4, 5, 6};
    Array<int> a = new Array<int> (false, true, 0);
    a.append_vals (x, x.length);
    a.append_vals (y, y.length);
    // taking over ownership avoids array copying
    int[] z = (owned) a.data; 

    foreach (var i in z) {
        stdout.printf ("%d ", i);
    }
    stdout.printf ("\n");

    return 0;
}

The Genie version:

[indent=4]

init
    x: array of int = {1, 2, 3}
    y: array of int = {4, 5, 6}
    var a = new Array of int (false, true, 0)
    a.append_vals (x, x.length)
    a.append_vals (y, y.length)
    z: array of int = (owned) a.data

Update: After answering this question I have modified the above code to use (owned) which avoids an uneccessary array copy operation.

The new Array<T> still adds some overhead for allocating an object, but that should be no problem in most cases.

Using Memory.copy is dangerous as it can cause all sorts of memory problems.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to concatenate two arrays?

From Dev

How to concatenate or merge two arrays

From Dev

how can I concatenate two arrays in javascript?

From Dev

How to vertically concatenate two arrays in Python?

From Dev

How to concatenate two byte arrays in C?

From Dev

How to concatenate two arrays using PHP?

From Dev

How to concatenate two or three arrays together in php?

From Dev

How do I concatenate two arrays in php?

From Dev

Concatenate values of two arrays

From Dev

Concatenate values of two arrays

From Dev

How to "merge" two arrays together? (Concatenate strings item-by-item)

From Dev

How can I concatenate two float arrays in Java?

From Dev

How concatenate two char arrays to open file from fopen in C?

From Dev

JNI How to concatenate two string arrays in C++ native function

From Java

Concatenate two NumPy arrays vertically

From Dev

Concatenate two SURFPoints arrays in Matlab

From Dev

How to concatenate arrays in bash?

From Dev

How to Concatenate Two LPCSTRs

From Dev

how concatenate two commands?

From Dev

how to push arrays in array and NOT concatenate?

From Dev

Concatenate each line of two arrays into a single one

From Dev

Android: Couldn't be able to Concatenate two Arrays

From Dev

Concatenate two arrays using void pointer (C)

From Dev

Concatenate elements of two string arrays jQuery

From Dev

Concatenate two arrays of different dimensions numpy

From Dev

How to concatenate arrays from cell arrays in Matlab

From Dev

How to concatenate two or more arrays in PHP without loosing values if it is same key and different values

From Dev

Concatenate numpy arrays to two arrays using pandas, numpy or other

From Java

How to concatenate two layers in keras?

Related Related

HotTag

Archive