- 1 A First C# Program
- 2 Namespaces
- 3 Alternative Forms of the Main() Function
- 4 Making a Statement
- 5 Opening a Text File for Reading and Writing
- 6 Formatting Output
- 7 The string Type
- 8 Local Objects
- 9 Value and Reference Types
- 10 The C# Array
- 11 The new Expression
- 12 Garbage Collection
- 13 Dynamic Arrays: The ArrayList Collection Class
- 14 The Unified Type System
- 15 Jagged Arrays
- 16 The Hashtable Container
- 17 Exception Handling
- 18 A Basic Language Handbook for C#
1.15 Jagged Arrays
Now that we've stored each line of text within an ArrayList container, we next want to iterate across the elements, separating each line into an array of the separate words. We'll need to store these arrays because they become fodder for the function implementing the word count. But this storage proves something of a problemor at least a puzzle. Problem or puzzle, jagged arrays provide a solution.
If we are only reading the elements of the container, the foreach loop is the preferred iteration method. It spares us the explicit cast of the object element to an object of its actual type. Any other element assignment requires the cast:
for( int ix = 0; ix < text.Count; ++ix ){ string str = ( string )text[ ix ]; // ... }
// read-only access ... foreach ( string str in text ){ ... }
Splitting one line of text into an array of its individual words is simple:
string [] words = str.Split( null );
The next part is also simple, but it is sometimes initially confusing. What we want to do is store the collection of these arrays themselves in an array. That is, we want an array of arrays.
The outer array represents the actual text. The array at the first index represents the first line, at the second index the second line, and so on. An ordinary multidimensional array cannot support what we want because it requires that both dimensions be fixed.
In our case, the first dimension is fixedit's the number of lines in the text filebut the second dimension varies with the number of words contained within each line of text. This is the situation a jagged array addresses. Each of its array elements can be an individual dimension. The syntax is an empty bracket pair for each dimension. For example, our array of arrays is two-dimensional, so its declaration looks like this:
string [][] sentences;
We initialize the array in two steps. In the first step we allocate the first dimension. This is the number of lines stored in the ArrayList object:
sentences = new string[ text.Count ][];
This statement says that sentences is an array of size text.Count that holds one-dimensional arrays of string elements. That is exactly what we want.
Next we need to individually initialize each of these elements with the actual string array. We'll do this by iterating across the ArrayList and assigning the resulting Split() of each of its strings:
string str; for( int ix = 0; ix < text.Count; ++ix ) { str = ( string )text[ ix ]; sentences[ ix ] = str.Split( null ); }
The individual string arrays are accessed through the first dimension. For example, to print out both the number of elements in the individual string arrays and the elements themselves, we could write the following:
// returns length of first dimension ... int dim1_length = sentences.GetLength( 0 ); Console.WriteLine( "There are {0} arrays stored in sentences", dim1_length ); for( int ix = 0; ix < dim1_length; ++ix ) { Console.WriteLine( "There are {0} words in array {1}", sentences[ ix ].Length, ix+1 ); foreach ( string s in sentences[ ix ]) Console.Write( "{0} ", s ); Console.WriteLine(); }
All the C# array types have access to the public members of the Array class defined in the System namespace. GetLength(), illustrated here, is one such member. The majority of the member functions, however, such as Sort(), Reverse(), and BinarySearch(), support arrays of only one dimension.