Array in JavaThis page is mainly for ISC Students on array address calculation.
An array is a list of a finite number of homogeneous data elements.The number of elements in an array is called the array length. Array length can be obtained from the index set by the formula :
Where UB is the largest index , called the upper bound and LB is the smallest index , called the lower bound. Suppose int Arr is an integer array. Upper bound of this array is 9 and lower bound of this array is 0 ,so the length is 9-0+1=10.
In an array ,the elements are stored successive memory cells.Computer does not need to keep track of the address of every elements in memory.It will keep the address of the first location only and that is known as base address of an array. Using the base address , address of any other location of an array can be calculated by the computer.Suppose Arr is an array whose base address is Base(Arr) and w is the number of memory cells required by each elements of the array Arr.The address of Arr[k] – k being the index value can be obtained by using the formula :
2 d Array :- Suppose Arr is a 2 d array.The first dimension of Arr contains the index set 0,1,2, … row-1 ( the lower bound is 0 and the upper bound is row-1) and the second dimension contains the index set 0,1,2,… col-1( with lower bound 0 and upper bound col-1.)
The length of each dimension is to be calculated .The multiplied result of both the lengths will give you the number of elements in the array.
Also Read: Computer Teacher at Burdwan
Representation of 2 d array in memory
Let’s assume Arr is an two dimensional 2 X 2 array .The array may be stored in memory one of the following way :-
1. Column by column I,e column major order
2. Row by row , I,e in row major order. The following figure shows both representation of the above array.
By row-major order, we mean that the elements in the array are so arranged that the subscript at the extreme right varies fast than the subscript at it’s left., while in column-major order , the subscipt at the extreme left changes rapidly , then the subscipt at it’s right and so on.
Column Major Order
Now we know that computer keeps track of only the base address. So the address of any specified location of an array , for example Arr[j,k] of a 2 d array Arr[m,n] can be calculated by using the following formula :-
(Column major order ) Address(Arr[j,k])= base(Arr)+w[m(k-1)+(j-1)]
(Row major order) Address(Arr[j,k])=base(Arr)+w[n(j-1)+(k-1)]
For example Arr(25,4) is an array with base value 200.w=4 for this array.The address of Arr(12,3) can be calculated using row-major order as
Address(Arr(12,3) )= 200+4[4(12-1)+(3-1)]
Again using column-major order
Address(Arr(12,3) )= 200+4[25(3-1)+(12-1)]