Sunday, May 26, 2013

Arrays






Array is the set of similar data type. In array first elemnt is stored at 0th location while last elemnt is stored at (n-1)th location. Elements within array are accessed by its index or location. There are two types of array:-
                   1) One-Dimensional Array:-
This type of array can be defined as two ways:
1) Type var_name[] = new type[size];
     This array will contains the size number of element. Size can be any whole number.
e.g.
      int x[] = new int[5];
In above type, array will contain 5 integer values or elements.
       char c[] = new char[10];
In this type, array will have 10 character elements.
2) Type var_name[] = {list of elements};
e.g.
      int x[] = {1,5,6,8,7};
In above type, array has above listed 5 integer values or elements.
      char s[] = {‘a’, ’d’, ’q’, ’r’};
In above type, array has above listed 4 charater values or elements.
           2) Multi-Dimensional Array:-
This type of array can also be defined as two types:-
         1) Type var_name[][] = new type[size][size];
         e.g.
               int x[][] = new int[5][4];
In above type, it will allocate 5 by 4 array of integer in two dimensions.
               char c[][] = new char[10][7];
In above type, it will allocate 10 by 7 array of character in two dimensions.
         2) Type var_name[][] = {{list of elements},{list of elements},….,{list of elemnts}};
         e.g.
               int x[][] = {{1,5,6,8,7},{15,7,98,35,354,58}};
In above type, it will allocate 5 by 6 array of above listed integers in two dimensions.
               char s[][] = {{‘a’, ’d’, ’q’, ’r’},{‘c’,’f’,’j’},{‘u’}};

In above type, it will allocate 4 by 3 by 1 array of above listed characters in three dimensions.

0 comments:

Post a Comment

    Translate

    Protected by Copyscape