Difference between revisions of "Arrays of Strings"

From CompSciWiki
Jump to: navigation, search
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. 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}}
  
 
== What Are Arrays of Strings? ==
 
== What Are Arrays of Strings? ==

Revision as of 14:33, 3 December 2007

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. 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.

   

{{{Body}}}

What Are Arrays of Strings?

As said in the introduction, we are mainly assuming that you know what an array is, and what a 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?

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.


How Do Arrays of Strings Work?

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.


How Do I Use Arrays of Strings?

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:

String ouchMyFace[];

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:

String ouchMyFace[] = new String[5]; // Creates an array of 5 Strings -- These Strings are currently null, since they don't exist!
                                     // 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!
ouchMyFace[0] = "Ouch,";
ouchMyFace[1] = "my";
ouchMyFace[2] = "face!";
ouchMyFace[3] = "It's";
ouchMyFace[4] = "on fire!";

// Now, let's print them. We'll assume you have seen loops.
// Note: Length is a variable in the array that counts how many elements are present
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 -- I am leaving it there because I am very lazy and it probably won't ever affect anything else.

Since your Strings are also arrays, you can also get at individual characters by using charAt(). Take this addition to our little 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?

As already has been shown, using arrays of Strings is fairly easy. There are many things to watch out for, however.

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 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.

String letsCrashThis[];

// Accessing an array element that does not exist: CRASH
letsCrashThis[5] = "An escalator cannot break. It can only become stairs.";

// This would cause the last statement to work, if placed before
letsCrashThis = new String[6];

// Accessing a String that does not exist, even though the array does: CRASH
System.out.println( letsCrashThis[3].charAt(0) );

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...

String moreCrashing[] = new String[10];

// Array off-by-one error: This accesses eleven elements, but the array has only ten!
// CRAAASSHH!
for( int i = 0; i <= 10; i++ )
    moreCrashing[i] = "Escalator Temporarily Stairs";

// Causes both array off-by-one errors AND String off-by-one errors
// 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) );

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.

--JonathanM 13:54, 27 November 2007 (CST)