Data Structures Arrays

Download Report

Transcript Data Structures Arrays

Data Structures
Arrays
M.KH
Arrays

An array is a container object that holds a fixed number of values of a single type.
The length of an array is established when the array is created. After creation, its
length is fixed

Each item in an array is called an element, and each element is accessed by its
numerical index. As shown in the preceding illustration, numbering begins with 0.
The 9th element, for example, would therefore be accessed at index 8.
Define Array

int[] a = new int[3];

int[] a = new int[]{10, 20, 30};
address
value
address
value
A[0]
0
A[0]
10
A[1]
0
A[1]
20
A[2]
0
A[2]
30

int[] a = new int[3];

for (int i = 0; i < a.length; i++)
a[i] = (i + 1) * 10;
Multidimensional Array

In the Java programming language, a multidimensional array is an array whose
components are themselves arrays. This is unlike arrays in C or Fortran. A
consequence of this is that the rows are allowed to vary in length
int[][] a = new int[2][3];
for(int i=0 ; i<a.length ; i++)
for(int j=0 ; j<a[i].length ; j++)
a[i][j] = (i + j) * 10;
Row-major and Column-major order

In computing, row-major order and column-major
order describe methods for arranging multidimensional arrays in
linear storage such as memory.

The difference is simply that in row-major order, consecutive
elements of the rows of the array are contiguous in memory; in
column-major order, consecutive elements of the columns are
contiguous.
Row-major and Column-major order
Row major order
Column major order
Row-major and Column-major order
Row major order
Column major order
Address
Value
Address
Value
0
11
0
11
1
12
1
21
2
13
2
12
3
21
3
22
4
22
4
13
5
23
5
23
Formula Column Major



In case of Column Major Order:
LOC (A [J, K]) = Base (A) + w [(J-L1) + M (K-L2)]
Here

LOC (A [J, K])
:
is the location of the element in the Jth row and Kth column.

Base (A)
:
is the base address of the array A.

w
:
is the number of bytes required to store single element of the array A.

M
:
is the total number of rows in the array.

J
:
is the row number of the element.

L1
:
is low bound of rows

K
:
is the column number of the element.

L2
:
is low bound of columns

A is 4x3 int array

Suppose we have to find the location of A [3, 1]. The required values are:

Base (A)
:
1000

w
:
4 (because an integer takes 4 bytes in memory)

M
:
4

J
:
3

L1
:
0

K
:
1

L2
:
0

Now put these values in the given formula as below:

LOC (A [3, 2]) = 1000 + 4 [(3-0) + 4 (1-0)]

= 1000 + 4 [3 + 4(1)]

= 1000 + 4 [7]

= 1028
Formula Row Major

In case of Row Major Order:

LOC (A [J, K]) = Base (A) + w [N (J-L1) + (K-L2)]

LOC (A [J, K])
:
is the location of the element in the Jth row and Kth column.

Base (A)
:
is the base address of the array A.

w
:
is the number of bytes required to store single element of the array A.

N
:
is the total number of columns in the array.

J
:
is the row number of the element.

L1
:
is low bound of rows

K
:
is the column number of the element.

L2
:
is low bound of columns

A is 4x3 int array

Suppose we have to find the location of A [2, 1]. The required values are:

Base (A)
:
1000

w
:
4 (because an integer takes 4 bytes in memory)

N
:
3

J
:
2

L1
:
0

K
:
1

L2
:
0

Now put these values in the given formula as below:

LOC (A [3, 2]) = 1000 + 4 [3 (2-0) + (1-0)]

= 1000 + 4 [3 (2) + 1]

= 1000 + 4 [7]

= 1028