Page 333 - ComputerScience_Class_11
P. 333

11.3.4 Length of the Array
                 The total number of elements an array holds is the length of the array. To find the total number of elements in the array,
                 we use the “length” property. This property returns an integer value. Syntax to use the length property is as follows:
                        int len= name_of_array.length;
                 Here, len will contain the length of the array. Let us consider the following example.


                   Program 7     Write a program to calculate the length of an array.


                   1       public class ArrayLength
                   2       {
                   3           public static void main(String[] args)

                   4           {
                   5               String player_name[ ]={"Sourav", "Sanchin", "Rahul", "Suresh", "Bumra"};

                   6               int length = player_name.length;
                   7               System.out.println("The length of the array is: " + length);

                   8           }
                   9       }


                 The output of the preceding program is as follows:

                         BlueJ: Terminal Window - Java
                     Options

                    The length of the array is: 5







                     11.4 DIFFERENT OPERATIONS ON A SINGLE-DIMENSIONAL ARRAY
                 Different types of operations which can be performed on an array are searching, sorting, insertion, deletion and
                 merging. Let us learn about them in detail.


                 11.4.1 Searching
                 Searching is used to determine whether an element is present in the array or not. In this class, we shall learn about
                 linear search and binary search.


                 Linear Search
                 The  other  name  for  linear  search  is  sequential  search.  Linear  search  works  efficiently  on  unsorted  data.  In  this
                 technique, the element to be found is searched from the 1st position of the array till the last position. If found, the
                 search will stop.
                 Let us take the following example in which ar is the name of the array of length 5.

                                 Position  or index  0           1           2           3          4
                                       Ar            2           4           5           7          9

                 Suppose, the number to be searched: 5



                                                                                                           Arrays  331
   328   329   330   331   332   333   334   335   336   337   338