Tuesday, 12 May 2020

Basic concept of Array in Java Program Part 1


 An Array is a group of like-typed variables that are referenced by a common name. 
                                                      OR
 An Array is the collection of Homogeneous or Similar data-type. 

Example 1
                                  // Use of integer in array 
class Array
{
    public static void main(String args[])
    {
        int[] A = new int[5] ;
        A[0] = 7 ;
        A[1] = 2 ;
        A[2] = 3 ;
        A[3] = 1 ;
        A[4] = 5 ;   
        System.out.println(" The value is " + A[2]) ;
        System.out.println(" The value is " + A[4]) ;
    }
}
=====================================================
Example 2
                                                // Addition in Array
class Array
{
    public static void main(String args[])
    {
        int[] A = new int[5] ;
        A[0] = 7 ;
        A[1] = 2 ;
        A[2] = 3 ;
        A[3] = 1 ;
        A[4] = 5 ;   
        int x = A[1] + A[4] ;        
        System.out.println(" The value is " + x) ;      
    }
}
====================================================
Example 3
                                       // Use of String in array 
class Array
{
    public static void main(String args[])
    {
        String d[] = {"Mon" , "Tue" , "Wed" , "Thurs" , "Fri" , "Sat"} ;       
        System.out.println(d[0]) ;
        System.out.println(d[2]) ;
    }
}



1 comment: