Friday 7 February 2014

Arrays

An array is a series of elements of the same type placed in contiguous memory locations. Elements in the array are accessed by using index positions.

Suppose if you want to calculate sum of 9 integers, you have to define 9 variables like below

    int var1, var2, var3, var4, var5, var6, var7, var8, var9;

    var1 = 5;
    var2 = -10;
    var3 = 7;
    var4 = 8
    var5 = 11;
    var6 = 12;
    var7 = 14;
    var8 = 21;
    var9 = 0;

By using new keyword we can declare like below

    int var[] = new int[9];

    var[0] = 5;
    var[1] = -10;
    var[2] = 7;
    var[3] = 8
    var[4] = 11;
    var[5] = 12;
    var[6] = 14;
    var[7] = 21;
    var[8] = 0;

As you see, Array indexes starts from 0. var[0] represents 5, var[2] represents 7 and so on.

continue                                                 One Dimensional Array                                                 Home

No comments:

Post a Comment