Cours
c#:Tableaux et matrices
Un cours complet pour maîtriser c#:Tableaux et matrices.
0 chapitres
0 QCM
Accès gratuit
c#:Tableaux et matrices
Généralités
Déclaration et création
int [ ] table1; char [ ] table2; float [ ] table3; string [ ] tableStr; .... table1 = new int [4]; table2 = new char [11]; table3 = new float [7]; tableStr = new string [8];
int [ ] table1= {13,-19,24,33,54,-97}; char [ ] table2= {'a','p','c','p','e','d','a','g','o','g','i','e'}; float [ ] table3= {-13.2, 35, -52.03, 3.543 ,57.57 }; ... string [ ] tableStr= {"apc","pedagogie","chemin","éclairant","cours"};Dans cette éventualité C# crée le tableau, calcule sa taille et l'initialise avec les valeurs fournies. Image de ce qui est présent en mémoire centrale après ces déclarations
Tableaux à deux dimensions(ou plus)
// Déclarer un tableau à deux dimensions (appelée aussi matrice)
int[,] tab1 = new int[2, 3]; // 2 lignes, 3 colonnes
//tableau multidimensions de string
string[,] tabString = new string[3, 2] { {"un", "deux"},
{"trois", "quatre"},
{"cinq", "six"} };
// Déclarer et définir les valeurs des éléments du tableau
int[,] tab2 = { { 1, 2, 3 }, { 4, 5, 6 } };
// déclaration d'un tableau de 10 lignes et de 20 colonnes
int[,] tableauRectangulaire = new int[10, 20];
// Tableaux à deux dimensions.
int[,] tab2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// The same array with dimensions specified.
int[,] tab2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
// Tableaux à deux dimensions string.
string[,] tab2Db = new string[3, 2] { { "un", "deux" }, { "trois", "quatre" },{ "cinq", "six" } };
// Tableau en trois dimensions.
int [,] tab3D = new int [,,] {{{1, 2, 3}, {4, 5, 6}},
{{7, 8, 9}, {10, 11, 12}}};
// Le même tableau avec des dimensions spécifiées.
int [,,] tab3Da = new int [2, 2, 3] {{{1, 2, 3}, {4, 5, 6}},
{{7, 8, 9}, {10, 11, 12}}};
Tableaux de tableaux (en escalier)
using System;
class Program
{
static void Main()
{
// Déclarer un tableau en escalier local avec 3 rangées .
int[][] jagged = new int[3][];
// Créer un nouveau tableau dans le tableau en escalier , et l'affecter .
jagged[0] = new int[2];
jagged[0][0] = 1;
jagged[0][1] = 2;
//Définir la deuxième rangée , initialisé à zéro .
jagged[1] = new int[1];
// Définir la troisième rangée , en utilisant tableau initialiseur .
jagged[2] = new int[3] { 3, 4, 5 };
//Imprimez tous les éléments du tableau déchiquetée .
for (int i = 0; i < jagged.Length; i++)
{
int[] innerArray = jagged[i];
for (int a = 0; a < innerArray.Length; a++)
{
Console.Write(innerArray[a] + " ");
}
Console.WriteLine();
}
}
}
//Résultats:
"1 2"
"0"
"3 4 5"