An array is defined as a finite set of homogeneous elements. Finite means, there is specific number of elements in the array. Homogeneous means, all the elements in the array must have the same type. The general form of array declaration in Java is

Data type array name [];

Or

Data type [] array name;

E.g. int a [];

Or

int [] a;

After the declaration we have to allocate memeory for the array in Java. The "new" operator is used to allocate memory.

Eg: int a[]=new int[10]; will create an integer array with 10 elements

Data type array name [];

Or

Data type [] array name;

E.g. int a [];

Or

int [] a;

After the declaration we have to allocate memeory for the array in Java. The "new" operator is used to allocate memory.

Eg: int a[]=new int[10]; will create an integer array with 10 elements

Note:In Java an integer variable will occupy 4 bytes of memory.

A linear or 1D array is a list of finite number of similar data elements. Each element of array is identified by a subscript(index). The elements of the array are stored respectively in successive memory locations.In Java the subscript start from 0.

The number of elements in an array is called the length or size of the array. In general the length(number of elements) of an array is obtained from the formula

Length= UB-LB+1

UB is the largest index called upper bound and LB is the smallest index called lower bound.

Let A be a linear array. The elements are stored in successive memory locations. The address of the first location is called base address. Using the base address, the address of any element of a can be calculated as:

A[i] =base address+ (i)*size;

size is the size of each individual element in the array.

Consider an array A having the base address 1000 and size of each element is 4 bytes. To find the address of the third element we have

A [3] =base adress+ (3)*size

=1000+ (3)*4

Address of A[3] =1012.

A 2D M X N array is a collection of M X N data elements such that each element is specified by a pair of integers I and J called subscript. The element of A with row subscript I and column subscript J is denoted by A [I ] [J].

The element A[I ] [J] appears in row I and column J

Let A be a 2D array with M rows and N columns. The array will be represented in memory by a block of M X N sequential memory location. 2D array can be stored either by column major order or by row major order.

A[0][0] A[0][1] A[0][2]

A[1][0] A[1][1] A[1][2]

A[2][0] A[2][1] A[2][2]

A[0,0]

A[0,1]

A[0,2]

A[1,0]

A[1,1]

A[1,2]

A[2,0]

A[2,1]

**One imensional(1D) Array**A linear or 1D array is a list of finite number of similar data elements. Each element of array is identified by a subscript(index). The elements of the array are stored respectively in successive memory locations.In Java the subscript start from 0.

The number of elements in an array is called the length or size of the array. In general the length(number of elements) of an array is obtained from the formula

Length= UB-LB+1

UB is the largest index called upper bound and LB is the smallest index called lower bound.

Represenation:Represenation:

Let A be a linear array. The elements are stored in successive memory locations. The address of the first location is called base address. Using the base address, the address of any element of a can be calculated as:

A[i] =base address+ (i)*size;

size is the size of each individual element in the array.

**Example**Consider an array A having the base address 1000 and size of each element is 4 bytes. To find the address of the third element we have

A [3] =base adress+ (3)*size

=1000+ (3)*4

Address of A[3] =1012.

**Two Dimensional (2D) Arrays**A 2D M X N array is a collection of M X N data elements such that each element is specified by a pair of integers I and J called subscript. The element of A with row subscript I and column subscript J is denoted by A [I ] [J].

The element A[I ] [J] appears in row I and column J

**Representation**Let A be a 2D array with M rows and N columns. The array will be represented in memory by a block of M X N sequential memory location. 2D array can be stored either by column major order or by row major order.

**Example:**A[0][0] A[0][1] A[0][2]

A[1][0] A[1][1] A[1][2]

A[2][0] A[2][1] A[2][2]

**Row Major Representation:**A[0,0]

A[0,1]

A[0,2]

A[1,0]

A[1,1]

A[1,2]

A[2,0]

A[2,1]

A[2,2]

In the row major organization the address of the element A[i][j] is computed as follows:

base address+j *n* size + i* size

A[0,0]

A[1,0]

A[2,0]

A[0,1]

A[1,1]

A[2,1]

A[0,2]

A[1,2]

A[2,2]

In column major order the address of the element A[i][j] is computed as follows

base adrees+ i* m *size + j* size

base address+j *n* size + i* size

**Column Major Representation:**A[0,0]

A[1,0]

A[2,0]

A[0,1]

A[1,1]

A[2,1]

A[0,2]

A[1,2]

A[2,2]

In column major order the address of the element A[i][j] is computed as follows

base adrees+ i* m *size + j* size

## Comments

## Post a Comment