# D語言數組

D編程語言提供了一種數據結構，數組用於存儲相同類型的元素的一個固定大小的連續集合。數組是用於存儲數據的集合，但它往往認爲陣列爲相同類型的變量的集合。

## 聲明數組：

type arrayName [ arraySize ];

double balance[10];

## 初始化數組：

double balance[5] = [1000.0, 2.0, 3.4, 17.0, 50.0];

double balance[] = [1000.0, 2.0, 3.4, 17.0, 50.0];

balance[4] = 50.0;

## 訪問數組元素：

double salary = balance[9];

import std.stdio; void main() { int n[ 10 ]; // n is an array of 10 integers // initialize elements of array n to 0 for ( int i = 0; i < 10; i++ ) { n[ i ] = i + 100; // set element at location i to i + 100 } writeln("Element Value"); // output each array element's value for ( int j = 0; j < 10; j++ ) { writeln(j," ",n[j]); } }

Element Value
0 100
1 101
2 102
3 103
4 104
5 105
6 106
7 107
8 108
9 109

## 靜態數組與動態數組

int[] dynamicArray;

## 數組屬性

.init

.sizeof

.length

.ptr

.dup

.idup

.reverse

.sort

import std.stdio; void main() { int n[ 5 ]; // n is an array of 5 integers // initialize elements of array n to 0 for ( int i = 0; i < 5; i++ ) { n[ i ] = i + 100; // set element at location i to i + 100 } writeln("Initialized value:",n.init); writeln("Length: ",n.length); writeln("Size of: ",n.sizeof); writeln("Yiibaier:",n.ptr); writeln("Duplicate Array: ",n.dup); writeln("iDuplicate Array: ",n.idup); n = n.reverse.dup; writeln("Reversed Array: ",n); writeln("Sorted Array: ",n.sort); }

Initialized value:[0, 0, 0, 0, 0]
Length: 5
Size of: 20
Yiibaier:7FFF5A373920
Duplicate Array: [100, 101, 102, 103, 104]
iDuplicate Array: [100, 101, 102, 103, 104]
Reversed Array: [104, 103, 102, 101, 100]
Sorted Array: [100, 101, 102, 103, 104]

## 多維數組

D編程允許多維數組。這裏是一個多維數組聲明的一般形式爲：

type name[size1][size2]...[sizeN];

int threedim[5][10][4];

## 二維數組：

type arrayName [ x ][ y ];

## 初始化二維數組：

int a[3][4] = [ [0, 1, 2, 3] , /* initializers for row indexed by 0 */ [4, 5, 6, 7] , /* initializers for row indexed by 1 */ [8, 9, 10, 11] /* initializers for row indexed by 2 */ ];

int a[3][4] = [0,1,2,3,4,5,6,7,8,9,10,11];

## 訪問二維數組元素：

int val = a[2][3];

import std.stdio; void main () { // an array with 5 rows and 2 columns. int a[5][2] = [ [0,0], [1,2], [2,4], [3,6],[4,8]]; // output each array element's value for ( int i = 0; i < 5; i++ ) for ( int j = 0; j < 2; j++ ) { writeln( "a[" , i , "][" , j , "]: ",a[i][j]); } }

a[0][0]: 0
a[0][1]: 0
a[1][0]: 1
a[1][1]: 2
a[2][0]: 2
a[2][1]: 4
a[3][0]: 3
a[3][1]: 6
a[4][0]: 4
a[4][1]: 8

## 常見的數組操作

### 數組分片

import std.stdio; void main () { // an array with 5 elements. double a[5] = [1000.0, 2.0, 3.4, 17.0, 50.0]; double[] b; b = a[1..3]; writeln(b); }

[2, 3.4]

### 數組複製

import std.stdio; void main () { // an array with 5 elements. double a[5] = [1000.0, 2.0, 3.4, 17.0, 50.0]; double b[5]; writeln("Array a:",a); writeln("Array b:",b); b[] = a; // the 5 elements of a[5] are copied into b[5] writeln("Array b:",b); b[] = a[]; // the 5 elements of a[3] are copied into b[5] writeln("Array b:",b); b[1..2] = a[0..1]; // same as b[1] = a[0] writeln("Array b:",b); b[0..2] = a[1..3]; // same as b[0] = a[1], b[1] = a[2] writeln("Array b:",b); }

Array a:[1000, 2, 3.4, 17, 50]
Array b:[nan, nan, nan, nan, nan]
Array b:[1000, 2, 3.4, 17, 50]
Array b:[1000, 2, 3.4, 17, 50]
Array b:[1000, 1000, 3.4, 17, 50]
Array b:[2, 3.4, 3.4, 17, 50]

### 數組設定

import std.stdio; void main () { // an array with 5 elements. double a[5]; a[] = 5; writeln("Array a:",a); }

Array a:[5, 5, 5, 5, 5]

### 數組連接

import std.stdio; void main () { // an array with 5 elements. double a[5] = 5; double b[5] = 10; double [] c; c = a~b; writeln("Array c: ",c); }

Array c: [5, 5, 5, 5, 5, 10, 10, 10, 10, 10]