- One Dimensional And Two Dimensional Array In C++
- Two Dimensional Array In Java
- One Dimensional Array Of Two Dimensional Array Is Called
- C Language Two Dimensional Array
- One Dimensional And Two Dimensional Array In C Image
I have a 49 space one dimensional array declared as int boardArray 49; and I also have a two dimensional 7x7 array declared as int boardArrayTwo 77' I am trying to use nested for loops to throw the one dimensional array into the two dimensional array here is the code I am using to test it. How to make a two-dimensional array. It helps to think of a two-dimensional array as a grid of rows and columns. An example of this type of array is a chess board — a grid of 8 rows and 8 columns. Though you can declare a single 64-element array to handle the job of representing a chess board, a two-dimensional array works better.
Back to: C#.NET Tutorials For Beginners and Professionals
2d Array in C# with Examples
In this article, I am going to discuss the 2d Array in C# with Examples. Please read our previous article before proceeding to this article where we discussed one-dimensional Arrays in C# with examples. As part of this article, we are going to discuss the following pointers which are related to Two-Dimensional Array in C#.
- What is Two-Dimensional Array in C#?
- Understanding the rectangular and jagged array in C# with examples.
What is Two-Dimensional Array in C#?
The arrays which store the elements in the form of rows and columns are called Two-Dimensional Array in C#. The two-dimensional array you can also say multidimensional array are of two types in C#. They are as follows
- Rectangular array: The array whose rows and columns are equal are called rectangular array
- Jagged array: The array whose rows and columns are not equal are called jagged array
Rectangular 2d Array in C#:
Let us first understand the syntax of Two-Dimensional Array in C#. Please have a look at the following diagram.
Let us see an example for a better understanding of the rectangular array in C#.
OUTPUT:
In the above example, we assigned the two-dimensional array element using nested for loop. It is also possible that we can assign the values to two-dimensional array in C# at the time of its declaration:
Assigning values to two-dimensional array in C# at the time of declaration:
OUTPUT:
Jagged Array in C#:
![One Dimensional And Two Dimensional Array In C One Dimensional And Two Dimensional Array In C](/uploads/1/1/9/5/119523879/123572110.jpg)
One Dimensional And Two Dimensional Array In C++
These are also two-dimensional array which will also store the data in the forms of rows and columns. But here in the jagged array, the column size will differ from row to row. That means if the first row contains 5 columns then the second row may contain 4 columns while the third row may contain 10 columns. So the point that you need to remember is if the column size varies from row to row then it is a jagged array. If the column size remains the same for all the rows then it is a rectangular two-dimensional array.
The jagged array in C# is also called the array of arrays. This is because in the case of the jagged array each row is a single dimensional array. So a combination of multiple single-dimensional arrays with different column sizes form a jagged array in C#.
Syntax: <type> [][] <name> = new <type> [rows][];
Example:
To declare a jagged array in C#, at the time of its declaration, you only need to specify the number of rows that you want in the array. for example
![Two Two](/uploads/1/1/9/5/119523879/121404883.png)
int [][] arr = new int[4][];
In the above array declaration, we are specifying that we want four rows in the array. Once you specify the number of rows that you want in the array, then you need to initialize each row with the number of columns by using a single dimensional array as shown below.
arr[0] = new int[5]; // we want five columns in the first row
arr[1] = new int[6]; // we want six columns in the first row
arr[2] = new int[4]; // we want four columns in the first row
arr[3] = new int[5]; // we want five columns in the first row
arr[1] = new int[6]; // we want six columns in the first row
arr[2] = new int[4]; // we want four columns in the first row
arr[3] = new int[5]; // we want five columns in the first row
Example of the jagged array in C#:
Two Dimensional Array In Java
When we run the application, it will give us the following output:
In the above example, we are assigning the elements of the jagged array by using nested for loop. It is also possible to assign the values of the jagged array at the time of its declaration.
Assigning jagged array at the time of its declaration:
One Dimensional Array Of Two Dimensional Array Is Called
OUTPUT:
C Language Two Dimensional Array
In the next article, I am going to discuss the advantages and disadvantages of arrays in C# with examples. Here, in this article, I try to explain the 2d Array in C# with examples. I hope this article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.