O'Reilly logo

C# Primer: A Practical Approach by Stanley B. Lippman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 problem—or 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 ]; // ... } // ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required