Java

Matrix in Java – 2D Arrays

While dealing with bulk data in Java, there can be a requirement for the developer to store the associated or related entries conveniently. For instance, accumulating the data at once that can be passed to any number of functions wherever needed. In such cases, the “2-dimensional” arrays in Java are assistive in streamlining the code functionalities and saving memory.

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:

int array[4][3]

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:

int[][] givenArray = {

 {10, 20, 30},

 {40, 50, 60, 70},

 {80, 90, 100, 110, 120},

};

System.out.println("The array elements are: ");

for (int i = 0; i < givenArray.length; ++i) {

 for(int j = 0; j < givenArray[i].length; ++j) {

System.out.println(givenArray[i][j]);

}}

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:

int[][] givenArray = new int[2][2];

givenArray[0][0] = 0;

givenArray[0][1] = givenArray[1][0] = 1;

givenArray[1][1] = 1;

System.out.println("The array elements are: ");

System.out.println(givenArray[0][0] + " " +givenArray[0][1]);

System.out.println(givenArray[1][0] + " " +givenArray[1][1]);

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

for(dT it : array) {

}

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:

String[][] givenArray = {

 {"Harry", "David"},

 {"Sara","Larry", "John"},

 {"Lina", "Jordan", "Paul", "Tim"},

};

System.out.println("The array elements are: ");

for (String[] innerArray: givenArray) {

for(String data: innerArray) {

System.out.println(data);

}}

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.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.