How to create an array in D language

Overview

An array in D is a language struct that can contain multiple values that can be of same data type or different types as indicated during declaration.

The declaration of arrays in D is different from languages like python, PHP, and the likes.

Create an array in D

Arrays in D can be staticLength is defined at declaration or dynamicLength is defined at execution time. They can also be pointer to dataArray whose value is obtained from it reference to another object. or associativeA key value pair type of array. Where a key, rather than an index points to a value..

These different types of arrays in D language are declared in different fashion and ways.

Code 1

Create a static array

Once we declare a static array, we can not increase the size along the way.

// import std.stdio
import std.stdio;
// main method
void main() {
// create as static array
int [3] stats = [70,80,100];
//try adding an extra fourth member, it throws an error.
//uncomment the line below to see the error.
// stats ~= 120;
// print the length of the static array
writeln(stats);
}

Explanation

  • Line 1 : We make the import package.
  • Line 5: We start the main function.
  • Line 7: we declare a static array stats and set its length as 3.
  • Line 14: We print to the screen.

Code 2

Create a dynamic array

Once we declare a dynamic array, we can keep adding as many values as we wish, thereby increasing the size along the way.

// import std.stdio
import std.stdio;
// main method
void main() {
// create as dynamic array
int [] carts = [70,80,100];
writeln("length before adding new member: ",carts.length);
//try adding an extra fourth member, it worked perfectly well.
carts ~= 120;
// print the length of the dyanmic array
writeln("length now, after adding new member : ",carts.length);
}

Explanation

  • Line 2: We make the import package.

  • Line 5: We start the main function.

  • Line 7: We declare a dynamic array carts and set its length as 3.

  • Line 8: We print the length of the array at first to the screen.

  • Line 10: We add a new value to our dynamic array.

  • Line 13: We print the length of the array after a new value is added to screen .

Code 3

Create an associative array

As shown in the code below, associative arrays have keys, integers of our choice that point or serve as indexes to a value. To access these values we can use their key in place of an index.

// import std.stdio
import std.stdio;
// main method
void main() {
// create as dynamic array
auto props = [70 :"shoes",80:"shirts", 100 : "electronics"];
//try adding an extra fourth member, it worked perfectly well.
writeln("Using key 70 to get value : ",props[70]);
writeln("Using key 80 to get value : ",props[80]);
writeln("Using key 100 to get value : ",props[100]);
}

Explanation

  • Line 2: We make the import package.

  • Line 5: We start the main function.

  • Line 7: We declare an associative array props and set its keys and values.

  • Line 10, 11, and 12: We print the values of the associative array using its keys to screen .

Code 4

Create a pointer to data array

To create this array, we must have access to the values of another variable. This can be achieved using the dereference operator (*). We’ll also indicate the value to point, using the ampersand (&). Below is an example of how it works.

In the code below, the pointer to data type of array can be seen as an array that holds a pointer to arrays:

// import std.stdio
import std.stdio;
// main method
void main() {
// create an array to be pointed
int [] arr = [70,80,100];
//point to the new array
int []* point = &arr;
//print the pointer value
writeln(point);
//print arr using the pointer
writeln(point[0]);
}

Explanation

  • Line 2: We make the import package.

  • Line 5: We start the main function.

  • Line 7: We declare an array arr and set its values.

  • Line 9: We declare a pointer array to hold the pointer values of arr.

  • Line 11: We print the pointer value.

  • Line 13: We display the value of arr using p, its pointer array to locate it.

In this shot, we learned by examples how we could easily create each of the different types of arrays in D language.

Free Resources