Page 425 - CA_Blue( J )_Class10
P. 425

16.4 STRING ARRAY
                 Till now, you have used different methods of the String class. Similar to character array, you can also create an array of
                 strings which is known as String array. This type of array is used to store multiple strings into a variable. You can access
                 a particular element (string) from a String array. You also access a particular character of an element of the String array
                 using the charAt() method. The syntax to create a String array:
                    String <array name>[] = {"value1", "value2", ...};
                 For example:
                    String str[] = {"Kolkata", "Delhi", "Mumbai", "Chennai", "Bengaluru"};
                 Here, String array name is str. It has 5 elements. You can access the value of a particular element by using the array
                 indexing method as shown below:
                    System.out.println(str[0]);
                 The preceding statement will display Kolkata.
                    System.out.println(str[4]);
                 The preceding statement will display Bengaluru. If you want to access a particular character of an element, then you
                 need to use the charAt() method with the array index, as shown below:

                    System.out.println(str[4].charAt(1));
                 The preceding statement will display the character on the 1st index of the element on the 4th index which is the letter e.

                 16.4.1 Graphical Representation of a String Array
                 The column index represents the individual character if an array element. On the other hand, the row index represents
                 a particular element of an array. Let us consider the preceding example:

                                         str   0     1     2     3     4     5     6     7     8
                                         0     K     o      l    k     a     t     a
                                         1     D     e      l    h      i
                                         2     M     u     m     b     a      i
                                         3     C     h     e     n     n     a      I
                                         4     B     e     n     g     a      l    u     r     u

                 To find the length of the array, the following code is used:
                        str.length                      // Will return 5
                 To find the length of the string of the first array element, the following code is used:

                        str[0].length()                 // Will return 7

                                                                                        21 st
                  Some More Programs                                                 Century   #Coding & Computational Thinking
                                                                                       Skills


                  Program 1    To input a sentence and count the number of words, alphabets and digits are there in the sentence.


                   1  import java.util.*;
                   2  class string_count
                   3  {

                   4      public static void main()

                   5      {
                   6          Scanner sc= new Scanner(System.in);



                                                                                                                       423
                                                                                                       String Handling  423
   420   421   422   423   424   425   426   427   428   429   430