Sunday, 29 January 2017

ARRAY IN JAVA

Array is a referenced data type used to create fixed number of multiple variables of same type to multiple values of similar type in continuous memory locations with single variable name. Suppose if we have 100 variables of same type in our program then we declare it in our program it occupies some memory location. So when we want to fetch those variables it would take time because all the variables may not be at same place. So here to overcome with such kind of problem we use the concept of an array which holds any number of variables of same type.

Let us take an example – Suppose there is a train called Rajdhani Express and it has different bogie, so we call engine as the first bogie and so on. Here in the case of Rajdhani Express, the train itself is an array because it is holding same kind of bogie as it is not holding other type of things like truck, aeroplane etc. So every array has some index number which start from 0 and increment by 1. In the case of train engine is the 0th bogie. So when we have to fetch records from an array we have to specify array name with its index number. Suppose in the train in 7th position bogie Harry is sitting. So to access Harry we have to specify as Rajdhani[6], as array index starts from 0 so 7-1 = 6.

Array Limitation – Its size is fixed, which means we cannot increase or decrease its size after its creation.

To declare array –
  • ·    int a[] = {1,2,3);  Here the array will automatically takes the size and it is compiler time array.
  • ·         int a[3];  This is runtime array where size is given and the elements we will input during runtime.


Example –
public class Array1 {
          public static void main(String[] args) {
                   // assigning array variable with values
                   int num[] = {5,10,2,4,8,6,1,7};
                   // fetching array values
                   for(int i=0;i<num.length;i++)
                   {
                             System.out.println(num[i]);
                   }
          }
}

Output –
5
10
2
4
8
6
1
7

Example –
public class Array2 {
          public static void main(String[] args) {
                   String name[] = {"rakesh","pramod","mukesh","vishwajeest"};
                   for(int i=0;i<name.length;i++)
                   {
                             System.out.println(name[i]);
                   }
          }
}

Output –
rakesh
pramod
mukesh
vishwajeet

Example –
import java.util.Scanner;
public class Array3 {
          public static void main(String[] args) {
                   Scanner sc = new Scanner(System.in);
                   int num[] = new int[4];
                   //assigning values
                   System.out.println("Enter the values : ");
                   for(int i=0;i<num.length;i++)
                   {
                             num[i]=sc.nextInt();
                   }
                   // fetching records
                   System.out.println("After fetched the values are : ");
                   for(int i=0;i<num.length;i++)
                   {
                             System.out.print(num[i]+" ");
                   }
          }
}

Output –
Enter the values :
5
1
3
8
After fetched the values are :
5 1 3 8

Example –
import java.util.Scanner;
public class Array4 {
          public static void main(String[] args) {
                   Scanner sc = new Scanner(System.in);
                   System.out.println("Enter the number of values");
                   int n = sc.nextInt();
                   int num[] = new int[n];
                   //assigning values
                   System.out.println("Enter the values : ");
                   for(int i=0;i<num.length;i++)
                   {
                             num[i]=sc.nextInt();
                   }
                   // fetching records
                   System.out.println("After fetched the values are : ");
                   for(int i=0;i<num.length;i++)
                   {
                             System.out.print(num[i]+" ");
                   }
          }
}

Output –
Enter the number of values
5
Enter the values :
1
4
7
8
5
After fetched the values are :
1 4 7 8 5

Example –
public class Array5 {
          public static void main(String[] args) {
                   String name[] = {"rakesh","pramod","mukesh","vishwajeet"};
                   for(int i=0;i<=name.length;i++)
                   {
                             System.out.println(name[i]);
                   }
          } 
}

Output –
rakesh
pramod
mukesh
vishwajeet
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
          at Array.Array5.main(Array5.java:9)


Note – Here in the above example we have 4 values in an array, and the position of an array starts from 0 and ends at n-1. So in the above example we are fetching records from 0th position and moves our counter till n<4 or n=4. As we have only 4 values in our array so the position is n-1 i.e. the counter should go till 3 and as we move our counter to 4 which mismatch our array size so it raise an error ArrayIndexOutOfBoundsException.

No comments:

Post a Comment