Thursday, June 26, 2014

Array - I



What are Arrays?

An array is a group of same typed variables that are referenced by a common name. Arrays can be any typed and one or more dimensions. A particular element can be addressed by array name and index of the array enclosed in third bracket ([]). Array can be declared with its initial values or can be defined with the size and can input the values latter. An array index start from 0 by default. Length of single dimension array can get using a function – array. length

int a[]={1,2,3,4};
double a[]=new double[10];

To store similar types of data under a common variable name, instead of declaring N number of variables for the N number of data, we use an array. An array is a collection of similar type of variables under a common variable. Thus we can have a collection or Array of integers, floats, characters etc., but remember that all elements of any given array must be of the same type, i.e. we cannot have an array of numbers some of which are int and some float etc. Either all should be int or all should be float or all should be char. The different components of an array are called the Array Elements. Arrays also known as subscripted variable
How to Declare an Array:
The array having one column and multiple rows are known as single dimensional array. Array with multiple rows and multiple columns are known as multi-dimensional arrays.
Like all other variables, an array needs to be declared first before it can be used. The declaration consists of stating the type of data-type used in the array, the name of the array and the number of elements in the array.
Single Dimensional Array

Data Type Array_Variable[ ] – new Data Type [Size] ;
Example :
int a[]=new int[20];
String name[]=new String[20];
Suppose we declare an array if integer with 5 elements, N[5] and when we store first five natural numbers to it, then it will look like the following table
N[0]
1
N[1]
2
N[2]
3
N[3]
4
N[4]
5
Here N is variable and [0] to [4] is subscript or element location. When we input data in N[0] to N[4] location we can recall back in any point in that particular program. Above array also known as single dimensional array. We can initialize the array with a list of values.
Example :
Java : int a[]={1,5,6,7};
String name[]={“Ashok”,”Anil”};
Initialization can be done when we use a fixed numbers of known data but there is no shortcut method to initialize large number of elements. 

Multi-Dimensional Array
A multi-Dimensional array declared with multiple rows and columns.
Int N[][] =  new int [3][3]
Above array will look like the following table.
Example


Column[0]
Column[1]
Column[0]





Row[0]

Cell[0][1]
Cell[0][1]
Cell[0][2]
Row[1]

Cell[1][1]
Cell[1][1]
Cell[1][2]
Row[2]

Cell[2][1]
Cell[2][1]
Cell[2][2]

We can initialize the Multi-dimensional array with a list of values too.
Int a[][]={{1,2,3},{3,4,5}};
String a[][]={{“A”,”B”},{“C”,”D”}}’;
Some example of Input and display the array elements.
Example




import java.util.*;
import java.util.Collections;
import java.util.Arrays;

   class Arr_Input_Output
     {
         public static void main(String[] args)
           {
               Arr_Input_Output AIR=new Arr_Input_Output();
               System.out.println("\n\tSingle Dimensional Array ");
               AIR.Single();
               System.out.println("\n\tSingle Dimensional Array ");
               AIR.Multi();              
           }
         public void Single()
           {
               Scanner Sc=new Scanner(System.in);
               int n;
               System.out.print("\n\tEnter Size : ");
               n=Sc.nextInt();
               int a[]=new int[n];
               System.out.println("\n\tEnter "+n+" Nos. : \n");
               for(int i=0;i
                 {
                     System.out.print("\t");
                     a[i]=Sc.nextInt();
                 }
               System.out.print("\n\tNos. in Array : "); 
               for(int i=0;i
                 {
                     System.out.print(a[i]+"  ");
                 }              
           }
         public void Multi()
           {
               Scanner Sc=new Scanner(System.in);
               int n,m;
               System.out.print("\n\tEnter Nos. of Rows : ");
               n=Sc.nextInt();
               System.out.print("\n\tEnter Nos. of Cols : ");
               m=Sc.nextInt();              
               int a[][]=new int[n][m];
               System.out.println("\n\tEnter "+(n*m)+" Nos. : \n");
               for(int i=0;i
                 {
                     for(int j=0;j
                       {
                           System.out.print("\t");
                           a[i][j]=Sc.nextInt();
                       }

                 }     
               System.out.print("\n\tNos. in Array : \n"); 
               for(int i=0;i
                 {
                     System.out.print("\t");
                     for(int j=0;j
                       {
                           System.out.print(a[i][j]+"  ");
                  
                       }
                     System.out.println(); 
                 }             
           }          
    }


Output


          Single Dimensional Array

          Enter Size : 3

          Enter 3 Nos. :

          1
          2
          3

          Nos. in Array : 1  2  3 
          Single Dimensional Array

          Enter Nos. of Rows : 2

          Enter Nos. of Cols : 2

          Enter 4 Nos. :

          1
          2
          3
          4

          Nos. in Array :
          1  2 
          3  4 






No comments: