Multidimensional Arrays
Two Dimensional Arrays
Structures in which each of the elements of an array are another array are called multidimensional arrays. For example, let's consider an array with 10 elements, and the elements of this array are arrays with 2 elements. This array will store a total of 20 pieces of data.
For example, we want students' number, name and surname information to be stored in a series without getting mixed up. A two-dimensional array would work for us. Suppose there are 20 students in the class. Since each element of the main array will carry 3 different information, namely number, name and surname, our array will have dimensions of 20 x 3.
Two-dimensional arrays can be likened to a matrix structure in mathematics or a table structure for easier understanding.
According to the example we need a 20 x 3 dimensional array. Each element of this array will also be a 3-element array. We define this sequence as follows.
string[ , ] my class = new string[ 20 , 3 ];
Next, let's assign values to the elements of the array. For example, let's enter the number, first and last name for the 0 element of the main array:
my class[ 0, 0 ] = "125";
my class[ 0, 1 ] = "Aysel";
my class[ 0, 2 ] = "Uysal";
We can assign other elements similarly:
my class[ 1, 0 ] = "135";
my class[ 1, 1 ] = "Arda";
my class[ 1, 2 ] = "Turan";
my class[ 2, 0 ] = "142";
my class[ 2, 1 ] = "Fatih";
my class[ 2, 2 ] = "Terim";
...
Examine the table below to understand the structure of this two-dimensional array and how we can access its elements.
0.0 |
0.1 |
0.2 |
1.0 |
1.1 |
1.2 |
2.0 |
2.1 |
2.2 |
3.0 |
3.1 |
3.2 |
4.0 |
4.1 |
4.2 |
5.0 |
5.1 |
5.2 |
It continues in the form. |
Multidimensional Arrays
In the two-dimensional arrays described above, if the elements of the inner arrays also contain other arrays, then multidimensional arrays emerge. A structure will be formed as if there is another painting in each eye of the table.
Let's change the above example a little more and consider it for a school . Let's have 5 classes in our school, 20 students in each class, and let's keep the numbers, names and surnames of all students.
In this case, we need a 3-dimensional array of dimensions 5 x 20 x 3.
string[ , , ] my class = new string[ 5, 20 , 3 ];
Note that we put two commas between the first square brackets because the array will be 3D.
Now let's assign values to the elements of the array.
my class[ 0, 0, 0 ] = "125";
my class[ 0, 0, 1 ] = "Arda";
my class[ 0, 0, 2 ] = "Turan";
my class[ 0, 1, 0 ] = "135";
my class[ 0, 1, 1 ] = "Fatih";
my class[ 0, 1, 2 ] = "Terim";
...
my class[ 0, 19, 0 ] = "958";
my class[ 0, 19, 1 ] = "Nihat";
my class[ 0, 19, 2 ] = "Kahveci";
...
my class[ 2, 5, 0 ] = "135";
my class[ 2, 5, 1 ] = "Fatih";
my class[ 2, 5, 2 ] = "Terim";
...
We can assign elements as.
c# multidimensional array, two dimensional array, three dimensional array, matrix
EXERCISES
There are no examples related to this subject.
Read 789 times.