epicours/Prog/Array.md

58 lines
1.1 KiB
Markdown

## Simple array (1 dimension)
An array is a vector in a collection
To declare an array we use :
```csharp
public static void Main() {
int[] vect = new int[4] // Declare a vector of 4 elements (from 0 to 3)
vect[3] = 22;
Console.WriteLine($"b = {b}");
Console.WriteLine($"{vect[3]}");
}
```
The result will be:
```bash
b = 0
22
```
We see that an array start from $0$ to $n-1$
## Complex array (x dimension)
To create a vect with multiple dimension, we just have to declare an array and add a brackets to define multiple dimension
```csharp
// First version
public static void Main() {
int[,] mat = new int[12, 9] // Declare an array of two dimensions mat with default value for [0]
mat[3, 4] = 12;
}
// Second version
public static void Main() {
int[,] mat = {
{ 2, 3 },
{ 4, 5 }
};
int a = mat[1, 0];
}
```
To display a matrice we will use:
```csharp
public static void Main() {
int[,] mat = {
{ 2, 3 },
{ 4, 5 }
};
int a = mat[1, 0];
for (int x = 0; x < math.GetLenght(...); x++) {
for (int y = 0; y < math.GetLenght(...); y++) {
Console.WriteLine(mat[x, y])
}
}
}
```