Difference between revisions of "Arrays of Strings"

From CompSciWiki
Jump to: navigation, search
(revamped)
Line 1: Line 1:
{{Template:1010Topic|Chapter_TOC=[[More With Arrays]] > [[Arrays of Strings]]|Introduction=This section is all about arrays of Strings. This section assumes that you have already read the chapter on Strings and the chapter on Arrays. In addition to teaching you the basics of arrays with Strings, I'll talk about indexing errors (such as off-by-one errors) and how infuriating they can be when using arrays of Strings.|Overview=After reading this section, you will know:<br>1. What an array of Strings is<br>2. How arrays of Strings work<br>3. Using arrays of Strings<br>4. Gotchas unique to arrays of Strings}}
+
{{Template:1010Topic|Chapter_TOC=[[More With Arrays]] > [[Arrays of Strings]]|Introduction=This section is all about arrays of Strings. This section assumes that you have already read the chapter on Strings and the chapter on Arrays. It will introduce you to arrays of Strings, show you how to use them, give you some tips of things to watch out for, and give you an idea of how they are stored.|Overview=After reading this section, you will know:<br>1. What an array of Strings is<br>2. How arrays of Strings work<br>3. Using arrays of Strings<br>4. Gotchas unique to arrays of Strings}}
  
== What Are Arrays of Strings? ==
+
== What are arrays of Strings? ==
  
As said in the introduction, we are mainly assuming that you know what an [[Arrays|array]] is, and what a [[Strings|String]] is. As a quick refresher, an array is a contiguous group of some data type, and a String is effectively (if not actually) an array of characters. So, where is this going?
+
Recall that an array is a contiguous group of some data type, and a String is text represented as a list of characters. An array of Strings, then, is a contiguous group of Strings, each of which contains text.
  
An array of Strings is a contiguous group of Strings, and each String is a contiguous group of characters. An array of strings is really a two-dimensional array: An array of arrays of characters. Even though you don't actually use Strings as arrays (for example, you get individual characters with charAt() instead of just an index), much of what you learn here will apply to real two-dimensional arrays. But that's for another course.
+
Also recall that a String is an object type, not a primitive type. That means that a String is represented by a reference that points to a value in memory. So, unlike arrays of primitive value types (like int, char, etc.) a String array is an array of references.
  
 +
== How do I use arrays of Strings? ==
  
== How Do Arrays of Strings Work? ==
+
String arrays are declared like any other array:
 
+
Even though a few of the concepts presented here aren't ''exactly'' the same as real multi-dimensional arrays, they are very, very similar.
+
 
+
We will begin with the first, big array "containing" each of these Strings. Here is where the definition of arrays as a contiguous "block" of a certain data type falls apart. The actual Strings being stored ''are not in this array'' -- Each of them is somewhere else in memory. Each slot of this big array just contains a number. This number is actually the memory address of the string that is "contained" in that array slot. This big array is an array of ''references'' to Strings. You will also learn about references in a later course, but for now, your Strings are accessed via those references. You will still work with your arrays of Strings as though they are contiguous groups of Strings, but note that they are really not contiguous groups of Strings.
+
 
+
Now, we will look at each String. The definition of arrays as contiguous groups of data now comes in to affect. We don't have individual characters scattered throughout memory and an array of references to them making up the String -- We actually do have contiguous blocks of characters.
+
  
 +
<pre>String[] ouchMyFace;</pre>
  
== How Do I Use Arrays of Strings? ==
+
You can access individual Strings in exactly the same way as you do with any other array. Observe:
 
+
Now that we have the brain-destroyingly confusing technical details of arrays of Strings out of the way, let's move on to actually using them. Using arrays of Strings is much, ''much'' simpler than trying to understand their low-level details. You declare them like any other, one-dimensional array:
+
 
+
<pre>String ouchMyFace[];</pre>
+
 
+
Of course, the confusing part is that you actually have a two-dimensional array, but all of the "smaller" arrays are contained within the String type and  you do not have to worry about them. Huzzah!
+
 
+
Anyway, you can get to your individual Strings in exactly the same way as you do with any other array. Observe:
+
  
 
<pre>
 
<pre>
String ouchMyFace[] = new String[5]; // Creates an array of 5 Strings -- These Strings are currently null, since they don't exist!
+
String ouchMyFace[] = new String[5]; // Creates an array of 5 Strings -- These Strings are initialized to null.
                                    // Again, you'll get to this in a later course, but note that there are no Strings here yet.
+
  
// So, let's make some Strings! Wheee!
+
// So, let's make some Strings!
 
ouchMyFace[0] = "Ouch,";
 
ouchMyFace[0] = "Ouch,";
 
ouchMyFace[1] = "my";
 
ouchMyFace[1] = "my";
Line 38: Line 25:
 
ouchMyFace[4] = "on fire!";
 
ouchMyFace[4] = "on fire!";
  
// Now, let's print them. We'll assume you have seen loops.
+
// Now, let's print them.
// Note: Length is a variable in the array that counts how many elements are present
+
// Note: length is a variable in the array that keeps track of the number of elements
 
for( int i = 0; i < ouchMyFace.length; i++ )
 
for( int i = 0; i < ouchMyFace.length; i++ )
 
     System.out.print( ouchMyFace[i] + " " ); // Prints a space after each String
 
     System.out.print( ouchMyFace[i] + " " ); // Prints a space after each String
 
</pre>
 
</pre>
  
The output of this little program will be the text "Ouch! My face! It's on fire! ". Note the space after the last exclamation point -- I am leaving it there because I am very lazy and it probably won't ever affect anything else.
+
The output of this little program will be the text "Ouch! My face! It's on fire! ". Note the space after the last exclamation point -- we could add additional code to fix this, if necessary.
  
Since your Strings are also arrays, you can also get at individual characters by using charAt(). Take this addition to our little program:
+
Since Strings contain more than one character, you can also get at individual characters by using charAt(). Observe the next part of our example program:
  
 
<pre>
 
<pre>
Line 58: Line 45:
 
== What Should I Watch Out For? ==
 
== What Should I Watch Out For? ==
  
As already has been shown, using arrays of Strings is fairly easy. There are many things to watch out for, however.
+
=== null Strings ===
  
The first thing I would like to mention is that you need to understand how arrays of Strings work "under the hood". Yes, the low-level details of String arrays probably makes you want to scream and panic, but those concepts are critical to debugging programs that work with arrays of Strings.
+
The first "gotcha" is that all Strings in an array of Strings is initialized to the value null. Null is a special value that means that a variable of an object type currently has no value; that is, its reference points to nothing. Null is not a valid String, so you must use '==' and '!=' to check for it, rather than the .equals() method, like in this example:
 
+
The second thing I would like to mention is that the additional dimension adds much more work. When using an array, you must first make sure that the array has been created with the '''new''' keyword. When using a String, you must first make sure that the String has been created with '''new''' (or by assigning a String literal to a String variable). From these, when you work with an array of Strings, you must first create the array of Strings, then you must go through the array and create each individual String. Note the examples in the last section: I created the array with '''new''', but this did not give me any Strings. I needed to go through each element of this array and create a String manually. Trying to access an array that does not exist, or a String that does not exist, will cause your program to throw an exception and halt.
+
  
 
<pre>
 
<pre>
String letsCrashThis[];
+
String[] testing = new String[2];
  
// Accessing an array element that does not exist: CRASH
+
testing[0] = "Not null!";
letsCrashThis[5] = "An escalator cannot break. It can only become stairs.";
+
  
// This would cause the last statement to work, if placed before
+
if (testing[0] == null) {
letsCrashThis = new String[6];
+
  System.out.println("testing[0] is null");
 +
}
 +
if (testing[1] == null) {
 +
  System.out.println("testing[1] is null");
 +
}
 +
</pre>
 +
 
 +
The output will be "testing[1] is null".
 +
 
 +
Calling any method on a String that does not point to a valid value (i.e. is null) will crash your program. The following program shows you what '''not''' to do:
 +
 
 +
<pre>
 +
String[] letsCrashThis = new String[5];
  
// Accessing a String that does not exist, even though the array does: CRASH
+
// Calling a method on a string that doesn't exist (i.e. is null)
 
System.out.println( letsCrashThis[3].charAt(0) );
 
System.out.println( letsCrashThis[3].charAt(0) );
 
</pre>
 
</pre>
  
The third thing I would like to mention is that you must watch your indexes. The additional dimension also adds so much more potential for array indexing errors. Not only can you try to access an element outside of your array of Strings, you can also try to access a character outside of a given String. This can get really annoying; see below for an example...
+
The output will be a run-time error.
 +
 
 +
=== Nested for loops ===
 +
 
 +
A String stores multiple characters, so unlike other arrays, we often need nested for loops when dealing with arrays of Strings.
 +
 
 +
Observe the difference between two methods that accomplish the same goal: counting the number of 'a' characters in an array.
 +
 
 +
In the first example, we are dealing with a character array.
  
 
<pre>
 
<pre>
String moreCrashing[] = new String[10];
+
public static int countNumberOfAs(char[] charArray) {
 +
  int count = 0;
  
// Array off-by-one error: This accesses eleven elements, but the array has only ten!
+
  for (int i=0; i < charArray.length; i++) {
// CRAAASSHH!
+
     if (charArray[i] == 'a') {
for( int i = 0; i <= 10; i++ )
+
      count++;
     moreCrashing[i] = "Escalator Temporarily Stairs";
+
    }
 +
  }
  
// Causes both array off-by-one errors AND String off-by-one errors
+
  return count;
// Access characters that are not part of each String,
+
}
// AND an array element that is not part of the array!
+
// See how insidious this can get?
+
for( int i = 0; i <= 10; i++ )
+
    System.out.println( moreCrashing[i].charAt(1000) );
+
 
</pre>
 
</pre>
  
This is all exactly why working with multi-dimensional arrays -- including arrays of Strings -- is so horrifying. Hopefully, this section has taught you to be especially vigilant when writing and debugging programs that work with arrays of Strings. If nothing else, at least you know to run screaming from any assignment that involves arrays of more than one dimension.
+
In the second example, we are dealing with a String array.
 +
 
 +
<pre>
 +
public static int countNumberOfAs(String[] stringArray) {
 +
  int count = 0;
 +
 
 +
  for (int i=0; i < stringArray.length; i++) {
 +
 
 +
    for (int j=0; j < stringArray[i].length(); j++) {
 +
      if (stringArray[i].charAt(j) == 'a') {
 +
        count++;
 +
      }
 +
    }
 +
 
 +
  }
 +
 
 +
  return count;
 +
}
 +
</pre>
 +
 
 +
Notice the addition nested for loop in the second example. When you are dealing with arrays of strings, you will often need nested loops like this because you have to deal with the characters in each String.
 +
 
 +
== How Do Arrays of Strings Work? ==
 +
 
 +
The actual Strings being stored ''are not in the array'' -- each of them is somewhere else in memory. Each element of the String array contains a reference that points to where the actual Strings are stored. You will still work with your arrays of Strings as though they are contiguous groups of Strings, but keep in mind that the elements of the array are references, not actual values.
 +
 
 +
== SIDENOTE: Two-dimensional arrays ==
 +
 
 +
The following information is not tested, but may help you understand arrays of Strings, and will be relevant to future computer science courses.
 +
 
 +
In Java and many other programming languages, Strings are stored as arrays of characters. That means that when you create an array of Strings, you are really creating an array of arrays of characters.
 +
 
 +
That's a bit of a mouthful; here is what it would look like in code:
 +
 
 +
<pre>
 +
// An array of 5 characters
 +
char[] charArray = new char[5];
 +
 
 +
// An array of 10 Strings
 +
String[] stringArray = new String[10];
 +
 
 +
// An array of 10 arrays of 5 characters (in many ways the same as an array of 10 Strings)
 +
char[][] char2DArray = new char[10][5];
 +
</pre>
  
--[[User:JonathanM|JonathanM]] 13:54, 27 November 2007 (CST)
+
A variable declared with two sets of square brackets ( [] ) like above is called a two-dimensional array. It is often used when dealing with information that makes sense as a matrix rather than a linear array.

Revision as of 02:31, 17 December 2008

COMP 1010 Home > More With Arrays > Arrays of Strings


Introduction

This section is all about arrays of Strings. This section assumes that you have already read the chapter on Strings and the chapter on Arrays. It will introduce you to arrays of Strings, show you how to use them, give you some tips of things to watch out for, and give you an idea of how they are stored.

   

{{{Body}}}

What are arrays of Strings?

Recall that an array is a contiguous group of some data type, and a String is text represented as a list of characters. An array of Strings, then, is a contiguous group of Strings, each of which contains text.

Also recall that a String is an object type, not a primitive type. That means that a String is represented by a reference that points to a value in memory. So, unlike arrays of primitive value types (like int, char, etc.) a String array is an array of references.

How do I use arrays of Strings?

String arrays are declared like any other array:

String[] ouchMyFace;

You can access individual Strings in exactly the same way as you do with any other array. Observe:

String ouchMyFace[] = new String[5]; // Creates an array of 5 Strings -- These Strings are initialized to null.

// So, let's make some Strings!
ouchMyFace[0] = "Ouch,";
ouchMyFace[1] = "my";
ouchMyFace[2] = "face!";
ouchMyFace[3] = "It's";
ouchMyFace[4] = "on fire!";

// Now, let's print them.
// Note: length is a variable in the array that keeps track of the number of elements
for( int i = 0; i < ouchMyFace.length; i++ )
    System.out.print( ouchMyFace[i] + " " ); // Prints a space after each String

The output of this little program will be the text "Ouch! My face! It's on fire! ". Note the space after the last exclamation point -- we could add additional code to fix this, if necessary.

Since Strings contain more than one character, you can also get at individual characters by using charAt(). Observe the next part of our example program:

// Note: length() is a method in String that returns the number of characters in the String
for( int i = 0; i < ouchMyFace[0].length(); i++ )
    System.out.print( ouchMyFace[0].charAt[i] ); // Gives you each individual character

This piece of code will print "Ouch,". Notice that there is no space at the end, since the original String had no spaces and we did not add any spaces.

What Should I Watch Out For?

null Strings

The first "gotcha" is that all Strings in an array of Strings is initialized to the value null. Null is a special value that means that a variable of an object type currently has no value; that is, its reference points to nothing. Null is not a valid String, so you must use '==' and '!=' to check for it, rather than the .equals() method, like in this example:

String[] testing = new String[2];

testing[0] = "Not null!";

if (testing[0] == null) {
  System.out.println("testing[0] is null");
}
if (testing[1] == null) {
  System.out.println("testing[1] is null");
}

The output will be "testing[1] is null".

Calling any method on a String that does not point to a valid value (i.e. is null) will crash your program. The following program shows you what not to do:

String[] letsCrashThis = new String[5];

// Calling a method on a string that doesn't exist (i.e. is null)
System.out.println( letsCrashThis[3].charAt(0) );

The output will be a run-time error.

Nested for loops

A String stores multiple characters, so unlike other arrays, we often need nested for loops when dealing with arrays of Strings.

Observe the difference between two methods that accomplish the same goal: counting the number of 'a' characters in an array.

In the first example, we are dealing with a character array.

public static int countNumberOfAs(char[] charArray) {
  int count = 0;

  for (int i=0; i < charArray.length; i++) {
    if (charArray[i] == 'a') {
      count++;
    }
  }

  return count;
}

In the second example, we are dealing with a String array.

public static int countNumberOfAs(String[] stringArray) {
  int count = 0;

  for (int i=0; i < stringArray.length; i++) {

    for (int j=0; j < stringArray[i].length(); j++) {
      if (stringArray[i].charAt(j) == 'a') {
        count++;
      }
    }

  }

  return count;
}

Notice the addition nested for loop in the second example. When you are dealing with arrays of strings, you will often need nested loops like this because you have to deal with the characters in each String.

How Do Arrays of Strings Work?

The actual Strings being stored are not in the array -- each of them is somewhere else in memory. Each element of the String array contains a reference that points to where the actual Strings are stored. You will still work with your arrays of Strings as though they are contiguous groups of Strings, but keep in mind that the elements of the array are references, not actual values.

SIDENOTE: Two-dimensional arrays

The following information is not tested, but may help you understand arrays of Strings, and will be relevant to future computer science courses.

In Java and many other programming languages, Strings are stored as arrays of characters. That means that when you create an array of Strings, you are really creating an array of arrays of characters.

That's a bit of a mouthful; here is what it would look like in code:

// An array of 5 characters
char[] charArray = new char[5];

// An array of 10 Strings
String[] stringArray = new String[10];

// An array of 10 arrays of 5 characters (in many ways the same as an array of 10 Strings)
char[][] char2DArray = new char[10][5];

A variable declared with two sets of square brackets ( [] ) like above is called a two-dimensional array. It is often used when dealing with information that makes sense as a matrix rather than a linear array.