Search code examples
arraysintappendd

How to add int to array of ints?


I need to add single int to array of ints. How can I do it with D?

void main()
{
    int v = 2;
    int [] x ~= v; // do not work
}

Working code:

int v = 2;
int [] x; 
x ~= v;

Solution

  • If you want to add a single int to an existing array, you must declare the array first:

    int[] x;
    x ~= 2;
    

    You can also initialize the array with a single int:

    int[] x = [2];