Two-dimensional arrays or multi-dimensional arrays are arrays where the data element's position is referred to, by two indices. The name specifies two dimensions, that is, row and column.
What Are Two-Dimensional Arrays?
Two-dimensional arrays can be defined as arrays within an array. 2D arrays erected as metrics, which is a collection of rows and columns. It is common to design 2D arrays to accomplish a database that is similar to the data structure.
The Need For Two-Dimensional Arrays
Using 2d arrays, you can store so much data at one moment, which can be passed at any number of functions whenever required.
Picture this, a class consists of 5 students, and the class has to publish the result of all those students.
You need a table to store all those five students' names, subjects' names, and marks.
For that, it requires storing all information in a tabular form comprising rows and columns.
A row contains the name of subjects, and columns contain the name of the students.
That class consists of four subjects, namely English, Science, Mathematics, and Hindi, and the names of the students are first, second, third, fourth, and fifth.
Declaration of Two-Dimensional Arrays
The syntax of two-dimensional arrays is:
Data_type name_of the array[rows][index];
Here is one example:
In the above example, the name of the 2d array is multi_dim consisting of 2 rows and three columns of integer data types.
Initialization of Two-Dimensional Arrays
There are two methods to initialize two-dimensional arrays.
Here are two methods of initialization of an element during declaration. Here, the second method is preferred because the second method is more readable and understandable so that you can clearly visualize that multi_dim 2D arrays comprise four rows and three columns.
Accessing Two-Dimensional Arrays
Accessing two-dimensional arrays can be done using row index value and column index value.
Suppose, in this example, you want to access element 80.
Note: indexing always starts with zero.
Printing the Elements in a Two-Dimensional Array
Printing elements of a two-dimensional array can be done using two for loops.
Want to begin your career as a Data Engineer? Check out the Data Engineering Courses and get certified.
Next, you will need to learn about more types of data structure like a linked list, stack, queue, tree, and graph, and many more.
If you are perhaps looking to learn further about data structures and gain a comprehensive understanding of data engineering, Simplilearn’s Post-Graduate Program in Data Engineering will be a great fit for you. Delivered by Simplilearn in partnership with Purdue University & in collaboration with IBM, this world-class bootcamp offers you the right skills and applied training you need to become a job-ready data engineering expert today. Take a look, this can prove to be the game changer for your career.
If you have any questions about this topic, please feel free to leave them in the comments below. Our team of experts will answer all your queries for you at the earliest!