Tuesday, 27 August 2019

Array


Array store similar data type values in an array variable.Arrays are of two types:

  • One dimensional arrays
  • Two dimensional arrays
Dis-advantage of array are:
  • Size is fixed(static array)
  • Stores only similar data types
We can overcome this disadvantage 
  • Size is fixed(static array):To overcome this problem we use collections--ArrayList, HashTable(dynamic array)
  • Stores only similar data types:To overcome this problem,we use Object array.
One dimensional arrays:

1:Integer array

Lowest bound/index= 0.
Upper bound/index= n-1(n is size of array).

        Example:
        int a[]={100,200,300,400,500}; 
        int i[]=new int [4];
        i[0]=10;
        i[1]=20;
        i[2]=30;
        i[3]=40;
        System.out.println(i[2]); //30
        System.out.println(i[3]); //40
        
 If we enter an index than the upper bound it throw Exception.
        System.out.println(i[4]); //ArrayIndexOutOfBoundsException

To get the size/length of array:
        System.out.println(i.length); //4
       
To print all the values of array we need to use for loop/For each loop
For Loop:
        for(int j=0;j<i.length;j++)
        {
            System.out.println(i[j]); //10,20,30,40
        }

For each Loop:
        for(int j:i)
        {
            System.out.println(j); //10,20,30,40
        }
       
2:Double array

        double d[]=new double[3];
        d[0]=10.33;
        d[1]=11.23;
        d[2]=14.28;
       
        System.out.println(d[2]); //14.28
       
3.Char array

        char c[]=new char[3];
        c[0]='s';
        c[1]=2;
        c[2]='$';
        for(char h:c)
        {
            System.out.println(h);
        }
       
4.Boolean Array

        boolean b[]=new boolean[2];
        b[0]=true;
        b[1]=false;
       
       
5.String array

        String s[]=new String[3];
        s[0]="test";
        s[1]="hello";
        s[2]="world";
        System.out.println(s.length);//3
       
6.Object array:(Object is class)
It is used to store different data types values

        Object s[]={10,12.556,welcome,F,true};
        Object ob[]=new Object[6];
        ob[0]="Tom";
        ob[1]=25;
        ob[2]=12.33;
        ob[3]="1/1/1990";
        ob[4]='M';
        ob[5]="London";
       
        System.out.println(ob.length);//6
        for(Object z:ob)
        {
            System.out.println(z);
        }

Two dimensional arrays:

        String x[][]=new String[3][5];

Total no of rows:
        System.out.println(x.length);//3 

Total no of columns:
        System.out.println(x[0].length);//5 

       1st Row:
        x[0][0]="A";
        x[0][1]="B";
        x[0][2]="C";
        x[0][3]="D";
        x[0][4]="E";
       2nd Row:
        x[1][0]="A1";
        x[1][1]="B1";
        x[1][2]="C1";
        x[1][3]="D1";
        x[1][4]="E1";
        3rd Row:
        x[2][0]="A2";
        x[2][1]="B2";
        x[2][2]="C2";
        x[2][3]="D2";
        x[2][4]="E2";
       
        System.out.println(x[1][2]); //C1

Two dimensional array can also initialize like:
int a[][]={{10,20,25},{30,40,45},{50,60,65}}
       
To print all values of two dimensional array we need to use two for loops:

        row=0,col=0 to 4
        row=1,col=0 to 4
        row=2.col=0 to 4
       
        for(int row=0;row<x.length;row++) {
            for(int col=0;col<x[0].length;col++)
            {
                System.out.println(x[row][col]);
            }


For each loop in two dimensional array:

        for(String i[]:x)
        {
            for(String j:i) {
                System.out.println(j);
            }
        }

No comments:

Post a Comment