This blog will demonstrate the utilization of “2D Arrays” in Java.
What is a Matrix in Java?
A “matrix” is a collection of numbers sorted into a fixed number of rows and columns. In order to represent this matrix using Java, a “2 Dimensional” array can be utilized.
What are “2D Arrays” in Java?
The “2D array” takes 2 dimensions, one for the row and the other for the column represented as follows:
The above declaration implies that the “array” contains “4” rows and “3” columns accumulating 4×3 = “12” integer values.
Example 1: Initializing and Printing the 2D Array Without Declaring the Size in Java
In this example, a “2D” array can be initialized without specifying its size and printed by iterating through “for” loop:
In this code, apply the following steps:
- Firstly, declare an integer array named “givenArray” without specifying its size.
- This array accumulates three arrays containing the given integer values.
- Lastly, print all the elements in the “2D” array via the “for” loop referring to the array and the nested “for” loop pointing to the array elements and the associated “length” property.
Output
In the above output, it is evident that all the array elements are displayed on the console.
Example 2: Initializing a 2D Array and Declaring its Size in Java
In the following example, each of the array elements can be initialized individually in accordance with its declared size:
In the above lines of code, apply the following steps:
- Define the integer array “givenArray” and specify its size.
- Note that the former size in “[ ]” indicates the “rows” in an array and the latter size corresponds to the “column” size.
- The specified size indicates that the maximum elements an array can contain are “2×2 = 4”.
- Lastly, assign the values against each array index one by one and display them.
Output
In this output, it can be seen that the array elements are assigned and displayed with respect to the allocated size.
Example 3: Initializing String Array and Accessing the Elements Via the “for…Each” Loop
This example can be utilized to initialize a “String” array and access its elements using the “for…Each” loop.
Syntax
}
In the above syntax:
- “array” refers to the array.
- “it” signifies the array item(s).
- “dT” indicates the data type of the array.
Let’s overview the following lines of code:
According to the above lines of code, perform the below-provided steps:
- Declare the string array “givenArray” having the stated string values and display them.
- Now, apply the “for…Each” loops.
- The former loop points to each of the individual arrays within the string array.
- The latter loop corresponds to the accumulated string elements in the contained arrays.
- Lastly, display the accessed array values.
Output
The above outcome implies that the “2D” string array is initialized and iterated appropriately.
Conclusion
A matrix in Java is a collection of numbers represented in a “2D” array that inputs two dimensions, one for the row and the other for the column. A 2-dimensional array can be initialized with or without declaring its size and can be iterated via the “for” or “for…Each” loop. This blog discussed the implementation of the “2D arrays” in Java.