Page 365 - CA_Blue( J )_Class10
P. 365

Program 1    To initialize an array and display its elements.


                   1  public class InitiArray
                   2  {

                   3      public static void main()
                   4      {

                   5          char vowels[] = {'a', 'e', 'i', 'o', 'u'};
                   6          for(int i = 0; i < 5; i++)

                   7          {
                   8              System.out.println(vowels[i]);

                   9          }
                  10      }

                  11  }

                 You will get the following output:
                 a
                 e
                 i
                 o
                 u
                 The for loop is used to iterate over the vowels array. Inside the body of the for loop, elements of the array are printed
                 one by one by using the index. When the loop executes first time, the value of i is 0. This value is passed to the vowels[i]
                 and the first value of array is printed. Similarly, all the values are printed.

                     15.3 LENGTH OF AN ARRAY
                 Length of an array means the total number of elements the array can store. To find it, a attribute named length is
                 provided by Java. Syntax to find the length of an array:
                   name_of_array.length;
                 Let us consider the example.
                   1  class array_length

                   2  {
                   3      public static void main()

                   4      {
                   5          int ar[]=new int[10];

                   6          int len;
                   7          len=ar.length;

                   8          System.out.println("Length of array is: " + len);
                   9      }

                  10  }


                                                                                                                       363
                                                                                                              Arrays   363
   360   361   362   363   364   365   366   367   368   369   370