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 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.
Different approaches for 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 in an array. e.g. no_of_rows = 3, then the array will have three rows.
- no_of_columns: The number of columns in an array. 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.
Below is the implementation of various approaches for 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 numbers and column numbers.
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.
Fundamental Subject in Computer Engineering: Data Structures & Algorithms Fundamental Subject in Mechanical Engineering: Fluid Mechanics Fundamental Subject in Electronics Engineering: Digital Electronics
Moreover, we can initialize each element of the array separately. Look at the code snippet below:
scores = 15 scores = 23 scores = 30 scores = 21 Printing 2D array using Arrays.deepToString() method: [[15, 23], [30, 21]]
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.
First three rows are: 0 1 2 3 4 1 2 3 4 5 2 3 4 5 6
Note: We can use arr. length function to find the size of the rows (1st dimension), and arr.length function 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 have a different number of columns. This type of array is called a Jagged Array.
The values of 2D jagged array 0 1 2 3 4 5
Program to add two 2D arrays:
Resultant 2D array: [5, 7, 9] [5, 8, 8]