Different Ways To Declare And Initialize 2-D Array in Java
An array with more than one dimension is known as a multi-dimensional array. The most commonly used multi-dimensional arrays are 2-D and 3-D arrays. We can say that any higher dimensional array is basically an array of arrays. A very common example of a 2D Array is Chess Board. A chessboard is a grid containing 64 1×1 square boxes. You can similarly visualize a 2D array. In a 2D array, every element is associated with a row number and column number. Accessing any element of the 2D array is similar to accessing the record of an Excel File using both row number and column number. 2D arrays are useful while implementing a Tic-Tac-Toe game, Chess, or even storing the image pixels.
Declaring of the 2-D array in Java:
Any 2-dimensional array can be declared as follows:
data_type array_name; (OR) data_type array_name;
- data_type: Since Java is a statically-typed language (i.e. it expects its variables to be declared before they can be assigned values). So, specifying the datatype decides the type of elements it will accept. e.g. to store integer values only, the data type will be declared as int.
- array_name: It is the name that is given to the 2-D array. e.g. subjects, students, fruits, department, etc.
Note: We can write [ ][ ] after data_type or we can write [ ][ ] after array_name while declaring the 2D array.
Initialization of 2-D array in Java:
data_type array_Name = new data_type[no_of_rows][no_of_columns];
The total elements in any 2D array will be equal to (no_of_rows) * (no_of_columns).
- no_of_rows: The number of rows an array can store. e.g. no_of_rows = 3, then the array will have three rows.
- no_of_columns: The number of rows an array can store. e.g. no_of_columns = 4, then the array will have four columns.
The above syntax of array initialization will assign default values to all array elements according to the data type specified.
Let us see various approaches of initializing 2D arrays:
Note: When you initialize a 2D array, you must always specify the first dimension(no. of rows), but providing the second dimension(no. of columns) may be omitted.
In the code snippet below, we have not specified the number of columns. However, the Java compiler is smart enough to manipulate the size by checking the number of elements inside the columns.
You can access any element of a 2D array using row number and column number.
In the code snippet below, we have not specified the number of rows and columns. However, the Java compiler is smart enough to manipulate the size by checking the number of elements inside the rows and columns.
Moreover, we can initialize each element of the array separately. Look at the code snippet below:
Using the above approach for array initialization would be a tedious task if the size of the 2D array is too large. The efficient way is to use for loop for initializing the array elements in the case of a large 2D array.
We can use arr. length can be used to find the size of the rows (1st dimension), and arr.length can be to find the size of the columns (2nd dimension).
Approach 5: (Jagged arrays)
There may be a certain scenario where you want every row to a different number of columns. This type of array is called a Jagged Array.
Let’s look at a simple program to add two 2D arrays:
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.