Friday 7 June 2013

ARRAYS IN JAVA

Array is a group of similar-type variables that are stored in a contiguous form in the memory. As they are stored in contiguous form, we can easily access all the elements of array using a common name. For accessing any particular element of array, we have to specify the index of that particular element. In java the indexing of any array strictly starts from 0 to n-1, where ‘n’ is the total number of elements of the array.

Consider the following program, it just declares an array of five elements and then accesses each element to print them.
SOURCE CODE # 1
/*************************************************************/
package arrays;

public class array_basic {
       public static void main(String[]args)
       {
              int arr[]=new int[5];
              arr[0]=25;
              arr[1]=78;
              arr[2]=94;
              arr[3]=49;
              arr[4]=16;
             
              for(int i=0;i<arr.length;i++)
                     System.out.println("arr["+i+"] = "+arr[i]);
             
       }

}

/*************************************************************/
Output:-
arr[0] = 25
arr[1] = 78
arr[2] = 94
arr[3] = 49
arr[4] = 16

Note that the name of the array in the above program is arr and each of the five elements of the array is accessed using the indexing which starts from 0. Here also note that that arr.length gives the total number of elements of the array.
Now consider the following program in which we initialize the array at the time of its declaration.
SOURCE CODE # 2
/**************************************************************/
package arrays;

public class array_initialisation {
       public static void main(String args[])
       {
              int arr[]={25,62,34,91,42};
              for(int i=0;i<arr.length;i++)
              {
                     System.out.println("arr["+i+"] = "+arr[i]);
              }
       }
}

/**************************************************************/
Output:-
arr[0] = 25
arr[1] = 62
arr[2] = 34
arr[3] = 91
arr[4] = 42

In the next program I would like to tell you about how to access array elements using advanced for loop or foreach loop.
SOURCE CODE # 3
/**************************************************************/
package arrays;
/*
 * accessing array elemennts using
 * enhanced for loop
 */
public class enhanced_for {
       public static void main(String[] args) {
              int arr[]={2,53,655,12,31,4};
              for(int value:arr)
              {
                     System.out.println(value);
              }
       }

}
/**************************************************************/
Output:-
2
53
655
12
31
4

If you are not familiar with the enhanced for loop visit this link.

The next thing that I would like to present is that how you can access all the array elements and then find the sum of all the elements of that array.
SOURCE CODE # 4
/**************************************************************/
package arrays;

public class sum_elements {
       public static void main(String[] args) {
              int arr[]={96,58,26,34,13};
              int sum=0;
              for(int i=0;i<arr.length;i++)
                     sum+=arr[i];
             
              System.out.println("The sum of all elements is : "+sum);
       }

}

/**************************************************************/
Output:-
The sum of all elements is : 227      

Now let’s have a look at simple program that illustrates the use of multidimensional arrays.
SOURCE CODE # 5
/**************************************************************/
package arrays;

public class multidimensional_arrays {

       public static void main(String[] args) {
             
              int arr[][]=new int [5][3];
             
              for(int i=0;i<5;i++)
              {
                     for(int j=0;j<3;j++)
                     {
                           arr[i][j]=i+j;
                     }
              }
             
              /*printing the array*/
             
              for(int i=0;i<5;i++)
              {
                     for(int j=0;j<3;j++)
                     {
                           System.out.println("arr["+i+"]["+j+"] = "+arr[i][j]);
                     }
              }
       }

}

/**************************************************************/
Output:-
arr[0][0] = 0
arr[0][1] = 1
arr[0][2] = 2
arr[1][0] = 1
arr[1][1] = 2
arr[1][2] = 3
arr[2][0] = 2
arr[2][1] = 3
arr[2][2] = 4
arr[3][0] = 3
arr[3][1] = 4
arr[3][2] = 5
arr[4][0] = 4
arr[4][1] = 5
arr[4][2] = 6


A different feature that java provides is that, here you can have your multidimensional array which consists of different number of columns for different rows.

SOURCE CODE # 6
/**************************************************************/
package arrays;

public class multi_dimensional2 {

      
       public static void main(String[] args) {
              int arr[][]={{9,8,7},{6,5},{4,3,2,1}};
             
              /* printing the array */
             
              for(int i=0;i<arr.length;i++)
              {
                     for(int j=0;j<arr[i].length;j++)
                     {
                           System.out.print(arr[i][j]+" ");
                     }
                     System.out.println();
              }
       }

}
/**************************************************************/
Output:-
9 8 7
6 5
4 3 2 1
In the above program the arr is a 2-dimensional array, in it’s first row it contains 3 columns, in the second row it contains 2 elements and in the third row that is the last row it contains 4 elements.1

In my last program I would like to tell you about how you can pass the whole arrays in the functions. After passing the whole array in a function you can then access each element of the array in that function.
SOURCE CODE # 7
/**************************************************************/
package arrays;
/*
 * incrementing each element of array using method
 */
public class arrays_in_mehtods {
       public static void main(String[]args)
       {
              int arr[]={45,52,63,95,30,12};
             
              System.out.println("Array before increment : ");
              for(int i=0;i<arr.length;i++)
                     System.out.println("arr["+i+"] = "+arr[i]);
             
              increment(arr);
             
              System.out.println("\nArray after increment : ");
              for(int i=0;i<arr.length;i++)
                     System.out.println("arr["+i+"] = "+arr[i]);
       }
      
       public static void increment(int a[])
       {
              for(int i=0;i<a.length;i++)
                     a[i]++;
       }
}
/**************************************************************/
Output:-
Array before increment :
arr[0] = 45
arr[1] = 52
arr[2] = 63
arr[3] = 95
arr[4] = 30
arr[5] = 12

Array after increment :
arr[0] = 46
arr[1] = 53
arr[2] = 64
arr[3] = 96
arr[4] = 31
arr[5] = 13

The above program contains a function which is basically used to increment each element of array by 1.

No comments:

Post a Comment