Arrays of Strings

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > More With Arrays > Arrays of Strings


Introduction

This chapter assumes that you have already read the chapter on Strings and the chapter on Arrays. This section includes what an array of Strings is, how to define and initialize an array of Strings, how to deal with null Strings and how to use String methods on an String Array indexes.


What are Arrays of Strings?

An array is a data structure whose indexes contain a specific data type. A String is an object where text is represented as a list of characters. An array of Strings is an array where each index contains a String object. Figure 1 displays an array of Strings containing brands of soft drinks.

Arrays.Strings.Fig.1.jpg

Define and Initialize an Array of Strings

You need to define an array of Strings before you can manipulate and process its contents. All String objects in an array of Strings are initialized to the value null the array of Strings is initialized. Null is a special value that signifies that an object type currently has no value. Figure 2 displays what our softDrinks array of Strings looked like when it was initialized.

 String[] softDrinks;  // defines a String array named "softDrinks"

softDrinks = new String[5];  // this initializes softDrinks to be an array of Size 5 

Arrays.Strings.Fig.2.jpg

The code below displays how to define, initialize and access indexes of the array of Strings shown in figure 1.

 public static void main(String[] args)
{
   // Option 1 for Defining, Initializing and Setting Contents
   String[] softDrinks;  // defines a String array named "softDrinks"

   softDrinks = new String[5];  // this initializes softDrinks to be an array of Size 5

   softDrinks[0] = "Coca Cola"; // sets index 0 of softDrinks to "Coca Cola"
   softDrinks[1] = "Pepsi"; // sets index 1 of softDrinks to "Pepsi"
   softDrinks[2] = "Dr. Pepper"; // sets index 2 of softDrinks to "Dr. Pepper"
   softDrinks[3] = "Sprite"; // sets index 3 of softDrinks to "Sprite" 
   softDrinks[4] = "7up"; // sets index 4 of softDrinks to "7up"
   softDrinks[5] = "Orange"; // sets index 5 of softDrinks to "Orange"

   // Option 2 for Defining, Initializing and Setting Contents
   String[] softDrinks2 = {"Coca Cola","Pepsi","Dr. Pepper","Sprite","7up","Orange");
} 


Using String Methods on Array Indexes

Each index of an array of Strings contains a String object. This means that all of the String methods you learned about can be applied to every index of an array of Strings.

Example 1: Print the length of every String in our SoftDrinks array (shown in Figure 1).

 public printLength(String[] softDrinks)
{
   for (int i = 0; i < softDrinks.length; i++)
   {
      System.out.println("Length of the String ",softDrinks[i]," is ",softDrinks[i].length());
   }
} 

Notice how we accessed the array index "softDrinks[i]", then applied the "length()" method to it. The length method was applied to every String object in our array softDrinks. You can also apply other String methods such as charAt and subString to the String objects in array indexes. Just remember to access the index of the String object first by using syntax like "softDrinks[i]".


Dealing with NULL Strings

Figure 3 shows the array resulting from the code below. Notice that there is a NULL value at index 4. This NULL value exists because we didn't assign anything to the String object at index 4.

 public static void main(String[] args)
{
   String[] animals;  // defines a String array named "animals"

   animals = new String[5];  // this initializes animals to be an array of Size 5

   animals[0] = "Cat"; // sets index 0 of animals to "Cat"
   animals[1] = "Dog"; // sets index 1 of animals to "Dog"
   animals[2] = "Bat"; // sets index 2 of animals to "Bat"
   animals[3] = "Bird"; // sets index 3 of animals to "Bird" 
   animals[5] = "Frog"; // sets index 5 of animals to "Frog"
} 

Arrays.Strings.Fig.3.jpg

The Problem with NULL Values

Let's print the contents of animals.

 public print(String[] animals)
{
   for (int i=0; i < animals.length; i++)
   {
      System.out.println(animals[i]);
   }
} 

We will encounter a problem when printing this array when i=4, because the String object at animals[4] is NULL. We can't print NULL, so our program will crash when i=4.

Fixing the NULL String Problem

We can fix our print method by testing for NULL values. The '==' and '!=' operators allow us to check each index to determine if it contains a NULL value or not. The code below shows off an improved version of our print method.

 public print(String[] animals)
{
   for (int i=0; i < animals.length; i++)
   {
      if (animals[i] != NULL)
      {
         System.out.println(animals[i]);
      }
   }
} 

When i=4 in our improved version, the if statement will evaluate and see that animals[4] == NULL and will not print. Therefor, we avoid our program crashing like our previous print method. The for loop will continue executing until the boolean i < animals2.length returns false.


Section Summary

Arrays of Strings are arrays where each index contains a String. You can process many Strings at once by utilizing an array of Strings instead of declaring a lot of separate String objects. All indexes within an array of Strings are initialized to NULL. You can manually assign String values to an array index to overwrite the NULL value. Be careful of NULL values when processing an array of Strings. Use the '!=' and '==' operators to check each index of an array of Strings for a NULL value. You are able to apply String methods to each index of an array of Strings. Make sure you access the index before applying the method. You now know what an array of Strings is, how to define and initialize an array of Strings, how to deal with null Strings and how to apply String methods to the indexes of an array of Strings.

Previous Page: Partially Filled Arrays Next Page: Searching Arrays