πŸŽ‰ Special Offer !    Code: GET300OFF    Flat β‚Ή300 OFF on every Java Course
Grab Deal πŸš€

Matrix Array in Java  


Introduction
  • A Matrix Array is a type of 2D array which stores data in a rows x columns format, just like a table or grid.
  • It is used to perform mathematical operations like addition, subtraction, multiplication, and finding determinants.
  • For example :
    • int[][] matrixArr = {
          {10, 20, 30},
          {40, 50, 60},
          {70, 80, 90}
      };
    • Matrix Array in Java

Working with Matrix Arrays:

  • To work with matrix arrays in Java, we need to follow these steps:
    1. Declare an array – Define the reference of the array with a specific data type.
    2. Create an array – Allocate memory for the array using the new keyword.
    3. Initialize an array – Assign values to the elements of the array.
    4. Retrieve elements of an array – Access array elements using their row and column index.
  • We will go through each step with examples below.
1. Declare Matrix Array
  • We can declare a matrix array in the same way as a 2D array.
    • Syntax:
      dataType[][] arrayName;
    • Example:
      int[][] numbers;
2. Create Matrix Array
  • We can create a matrix array by specifying rows and columns:
    • Syntax:
      arrayName = new dataType[rows][columns];
    • Example:
      numbers = new int[2][3];
3. Initialize Matrix Array
  • Array initialization means assigning values to matrix array.
  • After creation, all the index positions have default values (e.g., 0 for int, 0.0 for float etc).
  • We can initialize matrix array elements as below:
    • Syntax:
      arrayName[rowIndex][columnIndex] = value;
    • Example:
      int[][] numbers = new int[2][3];
      
      numbers[0][0] = 10;
      numbers[0][1] = 20;
      numbers[0][2] = 30;
      numbers[1][0] = 40;
      numbers[1][1] = 50;
      numbers[1][2] = 60;
4. Retrieve Elements of Matrix Array
  • Ways to retrieve elements from matrix array:
    1. Using Nested For Loop:
      for(int i = 0; i < numbers.length; i++) {
          for(int j = 0; j < numbers[i].length; j++) {
              System.out.println(numbers[i][j]);
          }
      }
    2. Using Enhanced For Loop (For-Each):
      for(int[] row : numbers) {
          for(int num : row) {
              System.out.println(num);
          }
      }

Matrix Array Programs:-
  • Program 1:
    public class MainApp1
    {
        public static void main(String[] args)
        {
            // 1. Declare, create, and initialize matrix array in a single line
            int[][] numbers = {
                {10, 20, 30},
                {40, 50, 60}
            };
    
            // 2. Access and print array elements using nested for-each loop
            System.out.println("Numbers are:");
            for (int[] row : numbers)
            {
                for (int num : row)
                {
                    System.out.print(num + " ");
                }
                System.out.println(); // Move to next row
            }
        }
    }
  • Program 2 (Addition of 2 Matrix Array):
    public class MatrixAddition
    {
        public static void main(String[] args)
        {
            // 1. Declare and initialize two 2x3 matrices
            int[][] matrix1 = {
                    {1, 2, 3},
                    {4, 5, 6}
            };
    
            int[][] matrix2 = {
                    {7, 8, 9},
                    {10, 11, 12}
            };
    
            // 2. Create a result matrix of the same size
            int[][] sum = new int[2][3];
    
            // 3. Perform matrix addition using nested for loop
            for (int i = 0; i < matrix1.length; i++)          // rows
            {
                for (int j = 0; j < matrix1[i].length; j++)   // columns
                {
                    sum[i][j] = matrix1[i][j] + matrix2[i][j];
                }
            }
    
            // 4. Print the result matrix
            System.out.println("Result of Matrix Addition:");
            for (int i = 0; i < sum.length; i++)
            {
                for (int j = 0; j < sum[i].length; j++)
                {
                    System.out.print(sum[i][j] + " ");
                }
                System.out.println(); // Move to next row
            }
        }
    }
    • Points to note:
      • We created two Matrix Arrays and a third array to store the sum.
      • Using nested for loops, we added corresponding elements of both matrices and stored them in the result matrix.