How To Create Matrix In Java

Hey guys!! Welcome to flower brackets blog. Today we are going to learn how to create matrix in java.

What is matrix in Java?

A matrix is similar to table with rows and columns. Java matrix can have any number of dimensions called matrices. In java most of the arrays are of single or two dimensions.

Also Read – Two Dimensional Array Java Program

NOTE : The datatype of java matrix elements must be same.

While defining a matrix (2d array row col) we should specify that the array has more than one dimension by putting square brackets after datatype.

Declaring multidimensional array java

int marks[] [] = new int [5] [5];  // declaring 2d array rows and columns

Here we have to remember that when an array is created it will be filled with zero by default if it is an integer array.

If you are declaring string array it will be filled by null values by default and “false” for boolean.

Use of nested loops is recommended while printing 2d array in matrix form in java.

Also Read – 2d Array Java

We are going create a matrix in java to set the borders of matrix equal to one and the rest equal to zero. To do this we have to open eclipse IDE and create a new class like this,

Example : how to create matrix in java

// java matrix

public class MatrixDemo
{
   public static void main(String[] args)
   {
      int matrix[][] = new int [4][4];
      matrixFunction(matrix);
   }
   
   public static void matrixFunction(int matrix[][])
   {
      for(int a = 0; a < 4; a++)
      {
         for(int b = 0; b < 4; b++)
         {
            if(a == 0 || b == 0 || a == 3 || b == 3)
            {
               matrix[a][b] = 1;
            }
            else
            {
               matrix[a][b] = 0;
            }
         }
      }

      for(int a = 0; a < 4; a++)
      {
         for(int b = 0; b < 4; b++)
         {
            System.out.print(matrix[a][b] + "    ");
         }
         System.out.println("    ");
      }
   }
}

In the above java matrix example I have created an array called “matrix” of type integer. Java array matrix is of four rows and four columns and that’s the size of the array.

Also Read – Array Java

Then I have created method matrixFunction and this will take in the array that we just created. In matrixFunction method I need two for loops. Because it’s two dimensional.

Inside second for loop I have created an if statement which will basically tell you the border conditions.

Also Read – Matrix Multiplication In Java

Because I want to make the borders of array equal to one. Else I want to make those cells equal to zero.

Outside for loop we are going to create two more for loops to print the array with little space between each number.

Finally I have called matrixFunction in the main method and if I run this program you can see like this,

How To Create Matrix In Java


Create a matrix in java – using scanner class

Let’s learn create matrix in java or how to create a matrix in java using scanner class.

import java.util.Scanner;

public class JavaMatrix 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in); 
      System.out.println("Please enter number of matrix rows : "); 
      int row = sc.nextInt(); 

      System.out.println("Please enter number of matrix columns : "); 
      int col = sc.nextInt();

      // declaring multidimensional array java
      int numbers[][] = new int[row][col];

      // filling java matrix
      fillingMatrix(sc, numbers, row, col);

      // printing 2d array in matrix form in java
      printingMatrix(numbers, row, col);
   }

   public static void fillingMatrix(Scanner scan, int num[][], int rows, int columns)
   {
      System.out.println("Please enter elements in matrix : ");

      for(int a = 0; a < rows; a++)
      {
         for(int b = 0; b < columns; b++)
         {
            num[a][b] = scan.nextInt();
         }
      }
   }

   public static void printingMatrix(int num[][], int rows, int columns)
   {
      System.out.println("Printing 2d array in matrix form in java : ");

      for(int a = 0; a < rows; a++)
      {
         for(int b = 0; b < columns; b++)
         {
            System.out.print(num[a][b] + "\t");
         } 
         System.out.println();
      }
   }
}

Output :

Please enter number of matrix rows :
3
Please enter number of matrix columns :
3
Please enter elements in matrix :
10
20
30
40
50
60
70
80
90
Printing 2d array in matrix form in java :
10 20 30
40 50 60
70 80 90


conclusion:

That’s it guys. This is all about how to create matrix in java. I hope you have understood create matrix in java.

You can subscribe to my blog flower brackets if you haven’t already. If you found this post useful share it across social networks.

Related Posts