
Two Dimensional Array in Java: Usage and Examples
Table of Content:
A multi-dimensional array is very much similar to a single dimensional array. It can have multiple rows and multiple columns unlike single dimensional array, which can have only one full row or one full column.
Syntax to Declare Multidimensional Array in java
datatype[][] identifier; dataType[][] arrayName;
or
datatype identifier[][];
Instantiation
arrayName = new dataType[rows][columns];
Combined Declaration and Instantiation
dataType[][] arrayName = new dataType[rows][columns];
Initialization with Values:
dataType[][] arrayName = { {value1, value2, value3}, {value4, value5, value6}, {value7, value8, value9} };
Example of 2D Array
Declaring and Initializing with Default Values
public class Main { public static void main(String[] args) { // Declaring and Instantiating int[][] array = new int[3][3]; // Filling values array[0][0] = 1; array[0][1] = 2; array[0][2] = 3; array[1][0] = 4; array[1][1] = 5; array[1][2] = 6; array[2][0] = 7; array[2][1] = 8; array[2][2] = 9; // Printing the array for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { System.out.print(array[i][j] + " "); } System.out.println(); } } }
Output:
1 2 3 4 5 6 7 8 9
Initializing with Values Directly
public class Main { public static void main(String[] args) { int[][] array = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // Printing the array for (int[] row : array) { for (int value : row) { System.out.print(value + " "); } System.out.println(); } } }
Output:
Example of Multidimensional java array
Program: Let's see the simple example to declare, instantiate, initialize and print the 2Dimensional array.
class Arrayxample{ public static void main(String args[]){ //declaring and initializing 2D array int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; //printing 2D array for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ System.out.print(arr[i][j]+" "); } System.out.print("\n"); } } }
Output:
1 2 3 2 4 5 4 4 5 Press any key to continue . . .
Java Programming Code for Two Dimensional (2D) Array
Following Java Program ask to the user to enter row and column size of the array then ask to the user to enter the array elements, and the program will display the two dimensional array:
/* Java Program Example - Two Dimensional Array */ import java.util.Scanner; public class JavaProgram { public static void main(String args[]) { int row, col, i, j; int arr[][] = new int[10][10]; Scanner scan = new Scanner(System.in); System.out.print("Enter Number of Row for Array (max 10) : "); row = scan.nextInt(); System.out.print("Enter Number of Column for Array (max 10) : "); col = scan.nextInt(); System.out.println("Enter " +(row*col)+ " Array Elements : "); for(i=0; i |
Output
When the above Java Program is compile and executed, it will produce the following output:
Enter Number of Row for Array (max 10) : 3 Enter Number of Column for Array (max 10) : 3 Enter 9 Array Elements : 1 2 3 4 5 6 7 8 9 The Array is : 1 2 3 4 5 6 7 8 9 Press any key to continue . . .
Addition of two matrices in java
Program: Let's see a simple example that adds two matrices.
class ArrayEx{ public static void main(String args[]){ //creating two matrices int a[][]={{2,4,6},{3,4,5}}; int b[][]={{7,1,2},{2,1,5}}; //creating another matrix to store the sum of two matrices int c[][]=new int[2][3]; //adding and printing addition of two matrices for(int i=0;i<2;i++){ for(int j=0;j<3;j++){ c[i][j]=a[i][j]+b[i][j]; System.out.print(c[i][j]+" "); } System.out.println();//new line } } }
Output:
9 5 8 5 5 10 Press any key to continue . . .
Program to multiply two matrices
Program: Let's see a simple example that multiply two matrices.
This java program multiply two matrices. Before multiplication matrices are checked whether they can be multiplied or not.
import java.util.Scanner; class MatrixMultiplication { public static void main(String args[]) { int m, n, p, q, sum = 0, c, d, k; Scanner in = new Scanner(System.in); System.out.println("Enter the number of rows and columns of first matrix"); m = in.nextInt(); n = in.nextInt(); int first[][] = new int[m][n]; System.out.println("Enter the elements of first matrix"); for ( c = 0 ; c < m ; c++ ) for ( d = 0 ; d < n ; d++ ) first[c][d] = in.nextInt(); System.out.println("Enter the number of rows and columns of second matrix"); p = in.nextInt(); q = in.nextInt(); if ( n != p ) System.out.println("Matrices with entered orders can't be multiplied with each other."); else { int second[][] = new int[p][q]; int multiply[][] = new int[m][q]; System.out.println("Enter the elements of second matrix"); for ( c = 0 ; c < p ; c++ ) for ( d = 0 ; d < q ; d++ ) second[c][d] = in.nextInt(); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) { for ( k = 0 ; k < p ; k++ ) { sum = sum + first[c][k]*second[k][d]; } multiply[c][d] = sum; sum = 0; } } System.out.println("Product of entered matrices:-"); for ( c = 0 ; c < m ; c++ ) { for ( d = 0 ; d < q ; d++ ) System.out.print(multiply[c][d]+"\t"); System.out.print("\n"); } } } }
Output:
Enter the number of rows and columns of first matrix 3 3 Enter the elements of first matrix 1 2 3 4 5 6 7 8 9 Enter the number of rows and columns of second matrix 3 3 Enter the elements of second matrix 9 8 7 6 5 4 3 2 1 Product of entered matrices:- 30 24 18 84 69 54 138 114 90 Press any key to continue . . .
- Question 1:
Declare a 2D array of integers in Java with 3 rows and 4 columns. Initialize the array with the following values:
1 2 3 4 5 6 7 8 9 10 11 12 - Question 2:
Given the following 2D array:
int[][] matrix = { {5, 8, 3}, {4, 9, 7}, {6, 2, 1} }; Write a program to display the element at the second row, third column.
- Question 3:
Write a Java program to calculate and print the sum of all elements in the following 2D array:
int[][] arr = { {2, 4, 6}, {1, 3, 5}, {7, 8, 9} }; - Question 4:
Write a Java program that takes a 2D array of size
2x3
and returns its transpose.1 2 3 4 5 6 Output:
1 4 2 5 3 6 - Question 5:
Write a Java program to search for a specific element in a 2D array and print its position (row and column index). If the element is not found, print a message indicating that the element was not found.
Given the following 2D array:
int[][] arr = { {5, 7, 9}, {4, 6, 8}, {1, 2, 3} }; - Question 6:
Write a Java program that calculates the sum of each row in a 2D array and prints the result.
Given the following array:
int[][] arr = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; - Question 7:
Write a Java program that prints the diagonal elements of a square 2D array.
Given the following array:
int[][] matrix = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; - Question 8:
Write a Java program to multiply two matrices. Ensure that the number of columns in the first matrix is equal to the number of rows in the second matrix.
Given matrices:
Matrix A (2x3):
1 2 3 4 5 6 Matrix B (3x2):
7 8 9 10 11 12 - Question 9:
Write a Java program to sort each row of the following 2D array in ascending order.
Example Input:
9 7 5 3 1 2 6 4 8 Expected Output:
5 7 9 1 2 3 4 6 8