Page 370 - CA_Blue( J )_Class10
P. 370
For example:
//For Integer array
int ar[][]= { {45, 67,34}, {45,50,14} };
byte ar[][]={ {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };
short ar[][]={ {1,1}, {1,2}, {2,2}, {3,3}, {3,4}, {4,4}, {5,5} };
long ar[][]={{1,23456, 23456, 76578345, 67567866};
//For real value array
float ar[][]={ {12.45f, 234.12f}, {45.56f, 11.33f} };
double ar[][]={ {1111.22, 2234.44}, {567.09, 56.33} };
//For character array
char ar[][] = { {'a','2'}, {':','A'} };
Sring ar[][]={ {"India", "12345"}, {"a123", "Class 10"} };
Note: Ensure that every row must contain equal number of elements while assigning values to an array.
Taking Values from User
There are two ways to take the values from the user which are:
• Using Method Argument: Using this method, you can take the values of an array as parameter at the time of
program execution. In this method, the array is passed as an argument to the main() method.
Program 5 To print a multi-dimensional array in the form of matrix.
1 public class matrix
2 {
3 public static void main(int mat[][])
4 {
5 int sum = 0, i, j;
6 System.out.println("Elements of mat[][] array are: ");
7 for(i = 0; i < 3; i++)
8 {
9 for(j = 0; j < 3; j++)
10 {
11 System.out.print(mat[i][j]+" ");
12 }
13 System.out.println();
14 }
15 }
16 }
368368 Touchpad Computer Applications-X

