Page 369 - CA_Blue( J )_Class10
P. 369
Declaring a Multi-Dimensional Array
Syntax to declare a multi-dimensional array is:
<data type> <array name> [][] = new <data type>[row size][column size];
For example:
int ar[][]= new int[2][4];
Preceding array can store eight elements as the number of rows X number of columns represents the number of
elements in multi-dimensional array. Indexes in multi-dimensional array are:
index 0 1 2 3
0 ar[0][0] ar[0][1] ar[0][2] ar[0][3]
1 ar[1][0] ar[1][1] ar[1][2] ar[1][3]
Representation of elements in a multi-dimensional array is shown below:
Array ar[][] of row size = 4 and col size = 3
columns
ar[][] 0 1 2
rows 0 10 20 30
1 100 200 300
2 1000 2000 3000
3 10000 20000 30000
In the above example, the name of the array is ar and 0,1,2 assigned horizontally are the column-wise index position
st
of the elements and 0,1,2,3 assigned vertically are row-wise index position. So, the 1 element is referred as ar[0][0]
containing the value 10, the next element row-wise is referred as ar[1][0] containing the value 100, and so on. Again
st
after the 1 element, the next element column-wise is referred ar[0][1] containing the value 20.
Let us declare arrays of different data types:
Array Types Data Types Format
byte byte ar[][]=new byte[2][3];
short short ar[][]=new short[2][4];
Integer array
int int ar[][]=new int[2][2];
long long ar[][]=new long[2][3];
float float ar[][]=new float[2][3];
Real numeric array
double double ar[][]=new double[2][2];
char char ar[][]=new char[2][3];
Character array
String String ar[][]=new String[2][2];
Initializing a Multi-Dimensional Array
Similar to single-dimensional array, there are two ways to initialize a multi-dimensional array. Let us discuss about
them.
Assigning Values Directly
You can assign the values to a multi-dimensional array at the time of its declaration between the curly brackets. The
syntax is as follows:
<data type> name_of_array[][] = {{val00, val01,…..},{val10,val11,….},{….}};
Where, a set of curly brackets { } represents a row in an array. Values of every row should be in separate set of curly
brackets and also separated by comma.
367
Arrays 367

