Page 352 - CITS - Computer Software Application -TT
P. 352
COMPUTER SOFTWARE APPLICATION - CITS
arr[2][1]=8;
arr[2][2]=9;
Example of Multidimensional Java Array
Let’s see the simple example to declare, instantiate, initialize and print the 2Dimensional array.
//Java Program to illustrate the use of multidimensional array
class Testarray3{
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.println();
}
}}
Output
1 2 3
2 4 5
4 4 5
Addition of 2 Matrices in Java
Let’s see a simple example that adds two matrices.
//Java Program to demonstrate the addition of two matrices in Java
class Testarray5{
public static void main(String args[]){
//creating two matrices
int a[][]={{1,3,4},{3,4,5}};
int b[][]={{1,3,4},{3,4,5}};
//creating another matrix to store the sum of two matrices
int c[][]=new int[2][3];
//adding and printing addition of 2 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
}
339
CITS : IT&ITES - Computer Software Application - Lesson 85 - 93