Exploring 2D Arrays in Java

The elements in a single dimensional array are represented by using a single row. If a number of single dimensional arrays each having same size are used together then the complete structure is termed as 'Double Dimensional Array'.
Hence, a double dimensional array (also called 2D array) is a structure created in memory to store the values in a matrix form. It is also called double subscripted variable because each of its element is represented by using two subscripts.

  • Basic Syntax of 2D Array:
  • DataType ArrayVariable[][]=new DataType[rows][columns]   //for example... int n[][]=new int[3][2];
  • Some programes using 2D Array:
  • Question-1Write a program to perform Addition of Two Matrices(3x3) using double dimensional array.

    Solution:

    import java.util.*; class PBAINST{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int a[][]=new int[3][3]; int b[][]=new int[3][3]; int A[][]=new int[3][3]; int i,j; System.out.println("Enter 1st Array:"); for(i=0;i<3;i++){ for(j=0;j<3;j++){ a[i][j]=sc.nextInt(); } } System.out.println("Enter the 2nd Array:"); for(i=0;i<3;i++){ for(j=0;j<3;j++){ b[i][j]=sc.nextInt(); } } System.out.println("Addition="); for(i=0;i<3;i++){ for(j=0;j<3;j++){ A[i][j]=a[i][j]+b[i][j]; System.out.print(A[i][j]+","); } System.out.println(); } } }
  • Some programes using 2D Array:
  • Question-2Write a program to perform Subtraction of Two Matrices(3x3) using double dimensional array.

    Solution:

    import java.util.*; class PBAINST{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int a[][]=new int[3][3]; int b[][]=new int[3][3]; int S[][]=new int[3][3]; int i,j; System.out.println("Enter 1st Array:"); for(i=0;i<3;i++){ for(j=0;j<3;j++){ a[i][j]=sc.nextInt(); } } System.out.println("Enter the 2nd Array:"); for(i=0;i<3;i++){ for(j=0;j<3;j++){ b[i][j]=sc.nextInt(); } } System.out.println("Subs="); for(i=0;i<3;i++){ for(j=0;j<3;j++){ S[i][j]=a[i][j]-b[i][j]; System.out.print(S[i][j]+","); } System.out.println(); } } }
  • Advantages of 2D Array:
  • 1.Structured Data Storage: 2D arrays provide a structured way to store data in rows and columns, which is especially useful for representing tabular or grid-like data.
    2.Efficient Access: Accessing elements in a 2D array is efficient and straightforward, as elements can be accessed using row and column indices.
    3.Matrix Operations:2D arrays are particularly suited for matrix operations such as addition, multiplication, and transposition, making them essential for mathematical computations and algorithms.
    4.Flexibility:Unlike fixed-size arrays, 2D arrays can be dynamically resized, allowing for flexibility in managing data.
    5.Easy to Understand and Manipulate:The grid-like structure of 2D arrays makes it easy to understand and manipulate data, simplifying tasks such as traversal, sorting, and searching.

  • Practices Exercise:
  • 1. create a 2D array called matrix with 3 rows and 4 columns using 2D Array.
    2.Write a program to perform the multiplication of two 3*3 matrices and display result.
    3.Write a program to interchange row and column elements of matrix i.e. transpose.


    Conclusion:
    In conclusion, 2D arrays in Java offer a versatile and efficient way to store and manipulate structured data in a grid-like format. Their advantages include organized storage, efficient access, flexibility in resizing, suitability for matrix operations, and ease of understanding and manipulation. Whether representing tabular data, implementing algorithms, or performing mathematical computations, 2D arrays provide a powerful tool for developers to manage data effectively in Java programs.