A set of similar data types is called an array these data types can be of any initial type. The java language provides a capability that enables the user to design a set of similar data types, called array. Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used to store a collection of data, but it is often more useful to think of an array as a collection of variables of the same type. Array work as a objects and objects default value is null.

Instead of declaring individual variables, such as number0, number1, ..., and number99, you declare one array variable such as numbers and use numbers[0], numbers[1], and ..., numbers[99] to represent individual variables. A specific element in an array is accessed by an index. All arrays consist of contiguous memory locations. The lowest address corresponds to the first element and the highest address to the last element.


One Dimensional array

It is a collection of data elements arranged in row is called 1 D array.


Array Declaration

To begin with, like other variables an array needs to be declared so that the compiler will know what kind of an array and how large an array we want. In our program we have done this with the statement:

int marks[5] ;


Accessing Elements of an Array

Syntax -

for ( i = 0 ; i <= 5 ; i++ ){

    cout<<”Enter marks ";

    cin>>marks[i];

}


Creating array 

It creates an array using new dataType[arraySize].

It assigns the reference of the newly created array to the variable arrayRefVar.

int a[] = new int[5];


Array Initialisation

So far we have used arrays that did not have any values in them to begin with. We managed to store values in them during program execution. Let us now see how to initialize an array while declaring it. Following are a few examples that demonstrate this.

Syntax - 

int num[6] = { 2, 4, 12, 5, 45, 5 } ;

int n[ ] = { 2, 4, 12, 5, 45, 5 } ;

float press[ ] = { 12.3, 34.2 -23.4, -11.3 } ;


Example - 

class A{

public static void main(String []arr){

int a[]={1,2,3,4,5};

int b[]={6,7,8};

a=b;

for(int i=0; i<a.length; i++){

System.out.println(a[i]++ +" " +b[i]);

}

}

}

Output 

        6    7

        7    8

        8    9


Example -

class A{

public static void main(String []arr){

A obj = new A();

int a[]={1,2,3,4,5};

obj.display(a);

for(int i=0; i<a.length; i++){

System.out.println(a[i]);

}

}

void display(int a[]){

int b[]={3,8,11};

b=a;

for(int i=0; i<a.length; i++){

System.out.println(b[i]++);

}

}

}

Output 

        1    2    3    4    5

        2    3    4    5    6


Two Dimensional array

It is a collection of 1D array and arranged in row and column are 2d array.

The simplest form of the multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays.


Array Declaration

How do we initialize a two-dimensional array? As simple as this...

int arr[2][2];


Array Initialisation

Multidimensioned arrays may be initialized by specifying bracketed values for each row. Following is an array with 3 rows and each row have 4 columns.

Syntax -

int arr[3][4] = {

    {0, 1, 2, 3} ,

    {4, 5, 6, 7} ,

    {8, 9, 10, 11}

};


Accessing Elements of an Array

An element in 2-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array. For example:

Syntax -

for ( i = 0 ; i <= 5 ; i++ ){

    for ( j = 0 ; j <= 5 ; j++ ){

        cin>>arr[i][j];

        cout<<”Enter marks";

        cout<<arr[i][j];

    }

}


Example - 

class A{

public static void main(String []arr){

int a[][]=new int[3][];

a[0]=new int[4];

a[1]=new int[2];

a[2]=new int[3];

for(int i[] : a){

for(int j : i){

System.out.print(j+" ");

}

System.out.println();

}

}

}

Output 

       0    0    0    0

        0    0

        0    0    0    


Example - 

class A{

public static void main(String []arr){

int a[]={9,4,1};

int b[][]={a, new int[2], a, {1,2,3,4}, {7,8}};

a=b[1];

b[4]=a;

for(int i=0; i<b.length; i++){

for(int j=0; j<b[i].length; j++){

System.out.print((b[i][j]++)+(a[i%2]++)+" ");

}

System.out.println();

}

}

}

Output

        9    5    3

        3    3

        14    10    8

        4    6    8    10

        15    16