Array

  • An array is a collection of similar data types.
  • Array is a container object that hold values of homogenous type. It is also known as static data structure because size of an array must be specified at the time of its declaration.
  •  Index of array starts from zero to size-1.
  • An array can be either primitive or reference type. It gets memory in heap area

Features of Array

  • It is always indexed. Index begins from 0.
  • It is a collection of similar data types.
  • It occupies a contiguous memory location

Flow Diagram

Flow Diagram of Array

Array Declaration

Syntax-

datatype identifier[];
        OR
 datatype[] identifier;

Example

int[] a;
float[] a;
char[] a;
short[] a;
int[][] a;

Initialization of Array

We used new operator to initialize an array.

int a[] =new int[5];
or
int a[]={2,3,4,5,6};

Accessing Array Element

In arrays, we can access the specific element by its index within square brackets.

Example

store[1]=5;

Multidimensional Array

Multidimensional arrays are arrays of arrays.

Array Declaration

int a[][] =new int[4][5];
or
int[][] a={{2,3,4,5,6}{7,8,9,10,1}};

Accessing Array Element

For specifying both row and column in array,index start with 0.

Syntax-

arrayname[n-1][m-1];

Example

arrayname[2][3]=5;