5 - Mátrix és tömbök tömbje

Többdimenziós tömb / mátrix

Megadott érték lekérése

<mátrix neve>[sor index, oszlop index]
int[,] matrix = {{11, 12}, {21, 22}, {31, 32}, {41, 42}}; Console.WriteLine(matrix[0, 1]); // Valasz: 12

Iterálás többdimenziós mátrixon

foreach (int <elem neve> in <mátrix neve>) { // Ciklusmag }
int[,] matrix = {{11, 12}, {21, 22}, {31, 32}, {41, 42}}; foreach (int elem in matrix) { Console.WriteLine(elem); }
int[,] matrix = {{11, 12}, {21, 22}, {31, 32}, {41, 42}}; for (int i = 0; i < matrix.GetLength(0); i++) { for (int j = 0; j < matrix.GetLength(1); j++) { Console.WriteLine(matrix[i, j]); } }

Tömbök tömbje

Megadott érték lekérése

<tömb neve>[sor index][oszlop index]
int[][] tomb = { new int[] {11, 12}, new int[] {21, 22, 23, 24}, new int[] {31, 32}, new int[] {41} }; Console.WriteLine(tomb[0][1]); // Valasz: 12

Iterálás tömbök tömbjén

int[][] tomb = { new int[] {11, 12}, new int[] {21, 22, 23, 24}, new int[] {31, 32}, new int[] {41} }; for (int i = 0; i < tomb.Length; i++) { for (int j = 0; j < tomb[i].Length; j++) { Console.WriteLine(tomb[i][j]); } }
VISSZA