Multidimensional Arrays in Java

Introduction to Multidimensional Arrays

Passing 2D Arrays to Methods and Returning Arrays

Copying Arrays - Shallow Vs Deep Copy

Introduction to Multidimensional Arrays in Java

Multidimensional arrays in Java are arrays that contain other arrays. This can be useful for storing data in a tabular form, such as a spreadsheet or a chessboard. To create a multidimensional array, you need to specify the type of elements that the array will store and the number of dimensions of the array. For example, the following code creates a two-dimensional array of integers:


  int[][] numbers = new int[3][4];
 

This code creates an array that has 3 rows and 4 columns. Each row of the array is an array of integers. To access the elements of a multidimensional array, you need to use the index of each dimension. For example, the following code accesses the element at row 1, column 2 of the numbers array:


  int element = numbers[1][2];
 

You can also use nested for loops to iterate over the elements of a multidimensional array. For example, the following code prints all of the elements of the numbers array to the console:


  for (int i = 0; i < numbers.length; i++) {
    for (int j = 0; j < numbers[i].length; j++) {
      System.out.println(numbers[i][j]);
    }
  } 

Multidimensional arrays are a powerful tool for storing and manipulating data in a tabular form. They are commonly used in Java programs for tasks such as:

  • Storing data for processing, such as the scores for a game or the prices of items in a store.
  • Implementing algorithms such as image processing and matrix multiplication.
  • Representing data structures such as graphs and trees.

Exercises

Code for reading and writing values to a 2D Array


package morningsession;
import java.util.*;
public class first 
{
public static void main(String[] args) 
{
  
  Scanner input = new Scanner(System.in);
  int a[][]= new int[2][2];
  
  for(int i=0;i<a.length;i++)
  {
    for(int j=0;j<a[i].length;j++)
    {
      a[i][j]=input.nextInt();
    }
  }

  for(int k[]:a)
  {
    for(int j:k)
    {
      System.out.println(j);
    }
  }
}	
}     

Check if a matrix is an identity matrix in Java. Get the input from the user.


  package morningsession;
  import java.util.*;
  public class first 
  {
    public static void main(String[] args) 
    {
          System.out.println("Enter numbers");
        Scanner input = new Scanner(System.in);
        boolean identity=true;
      int a[][] = new int[2][2];
      int i=0,j=0;
      for(i=0;i<a.length;i++)
      {
        for(j=0;j<a[i].length;j++)
        {
          a[i][j]=input.nextInt();
        }
      }
      //Check if its an identity matrix 
      for(i=0;i<a.length;i++)
      {
        for(j=0;j<a[i].length;j++)
        {
          if((i==j && a[i][j]!=1) || (i!=j && a[i][j]!=0))
          {
            System.out.println("not an identity");
            identity=false;
            break;
          }
        }
        if(!identity)
        {
          break;
        }
      }
      
      if(identity)
      {
        System.out.println("Its an identity matrix");
      }
        
    }	
  }  

Determine the sum of two matrices in Java. Get the input from the user.


  package morningsession;
  import java.util.*;
  public class first 
  {
  public static void main(String[] args) 
  {
    System.out.println("Enter numbers");
    Scanner input = new Scanner(System.in);
    boolean identity=true;
    int a[][] = new int[2][2];
    int b[][] = new int[2][2];
    int result[][] = new int[2][2];
    int i=0,j=0;
    System.out.println("Matrix 1 input");
    for(i=0;i<a.length;i++)
    {
      for(j=0;j<a[i].length;j++)
      {
        a[i][j]=input.nextInt();
      }
    }
    System.out.println("Matrix 2 input");
    for(i=0;i<a.length;i++)
    {
      for(j=0;j<a[i].length;j++)
      {
        b[i][j]=input.nextInt();
      }
    }
    //sum of two matrices
    for(i=0;i<a.length;i++)
    {
      for(j=0;j<a[i].length;j++)
      {
        result[i][j]=a[i][j]+b[i][j];
      }
    }
    
    for(int k[]:result)
    {
      for(int m:k)
      {
        System.out.print(m);
      }
      System.out.println();
    }
  }	
  } 

Determine the transpose of a 2*2 matrix. Get the input from the user.


package morningsession;
import java.util.*;
public class first 
{
  public static void main(String[] args) 
  {
      Scanner input = new Scanner(System.in);
      int a[][] = new int[2][2];
    int b[][] = new int[2][2];
    int i=0,j=0;
    System.out.println("Matrix input");
    for(i=0;i<a.length;i++)
    {
      for(j=0;j<a[i].length;j++)
      {
        a[i][j]=input.nextInt();
      }
    }
    System.out.println("Matrix 2 input");
    for(i=0;i<a.length;i++)
    {
      for(j=0;j<a[i].length;j++)
      {
        b[j][i]=a[i][j];
      }
    }
  
    for(int k[]:b)
    {
      for(int m:k)
      {
        System.out.print(m);
      }
      System.out.println();
    }
  }	
} 

Copying an array using the clone method.


  package morningsession;
  import java.util.*;
  public class first 
  {
    public static void main(String[] args) 
    {
      int a[]=new int[] {1,2,3,4,5,6,7};
      int b[]=new int[a.length];
      b=a.clone();
      for(int k:b)
      {
        System.out.println(k);
      }
      
    }
  } 

Copying an array using the arraycopy method


package morningsession;
import java.util.*;
public class first 
{
  public static void main(String[] args) 
  {
    int a[]=new int[] {1,2,3,4,5,6,7};
    int b[]=new int[3];
    System.arraycopy(a, 4, b, 0, 3);
    for(int k:b)
    {
      System.out.println(k);
    }   
  }
} 

SearchinCode for performing a deep copy of two 2D arrays


  package morningsession;
  import java.util.*;
  public class first 
  {
    public static void main(String[] args) 
    {
      int a[][]= {{1,1},{2,2}};
      int b[][]=new int[a.length][a[0].length];
      for(int i=0;i<a.length;i++)
      {
        for(int j=0;j<a[i].length;j++)
        {
          b[i][j]=a[i][j];
        }
      }
      a[0][0]++;
      for(int k[]:b)
      {
        for(int m:k)
        {
          System.out.println(m);  
        } 
      }
    }
  }