|
|
|
Summary
This chapter covered the
System.String
class in detail: It is used to create formatted strings and locate
substrings. You
|
|
|
|
|
|
|
Further Reading
Programming C# , Third Edition by Jesse Liberty , O'Reilly. ISBN: 0596004893. The Applied Microsoft .NET Framework Programming in C# Collection , Microsoft Press. ISBN: 0735619751. Mastering Regular Expressions , Second Edition by Jeffrey Friedl , O'Reilly. ISBN: 0596002890. Regular Expression Pocket Reference by Tony Stubblebine , O'Reilly. ISBN: 059600415X. |
|
|
|
|
|
|
Chapter 5. ARRAYS AND COLLECTIONSIN BRIEF
This chapter explains and
WHAT YOU NEED
ARRAYS AND COLLECTIONS AT A GLANCE
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
|
|
|
Using ArraysAn array is simply a list or collection of items. Every array in the Common Language Runtime inherits from the System.Array class. Table 5.1 lists the terminology commonly used when talking about arrays. Table 5.1. Array Terminology
In the following sections, you will learn how to create single-dimensional and multidimensional arrays. Then you will learn how to create and use a jagged array (array of arrays). Finally, you will learn how to pass an array as a parameter. Understanding Single-Dimensional and Multidimensional Arrays
Single-dimensional and multidimensional arrays
are exactly what their
Single-Dimensional Array variable-type [] variableName = new v ariable-type [length] For multidimensional arrays, the form is only slightly different. You simply add a comma (,) in the declaration. One comma declares it as a two-dimensional array. Two commas declare it as a three-dimensional array, and so on. Two-Dimensional Array variable-type [,] variableName = new v ariable-type [length, length]
If you want to create an array using late
binding, the static method
System.Array.CreateInstance
is
the correct choice. The following code snippet declares and
int[] intArray = new int[10];
In the
Listing 5.1. Storing the Months of the Year in a Single-Dimensional Array
using System;
namespace SimpleArrays
{
class SimpleArray
{
static private string[] months =
new string[12] { "Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
foreach(string month in months)
{
System.Console.WriteLine(month);
}
System.Console.ReadLine();
}
}
}
Listing 5.2
System.Array.GetLowerBound(int dimension) System.Array.GetUpperBound(int dimension) System.Array.Rank By adding the System.Array.GetUpperBound method call to the for loop, you can determine the length of the specified dimension and iterate through all items in that dimension. Listing 5.2. Storing the Months of the Year Along with the Days in the Month in a Two-Dimensional Array
using System;
namespace TwoDimensionalArrayExample
{
/// <summary>
Explaining Jagged ArraysA jagged array is simply an array of arrays. It takes a little different form from that of a two-dimensional array in that the dimensions are no longer separated by a comma. Think of a standard two-dimensional array as rectangular and a jagged array as having an inconsistent shape, with each element of the array containing a different number of sub-elements. Now each dimension has its own set of brackets. The following code snippet demonstrates how to declare and initialize a jagged array of integers:
int[][] intArray=new int[][] { new int[] {1,3,7,9},
new int[] {2,4,6,8,10}
}
Listing 5.3 modifies the example introduced in the multidimensional array to use a jagged array instead of a two-dimensional array. Listing 5.3. Jagged Array Example
using System;
namespace JaggedArrayExample
{
class JaggedArray
{
static private string[][] months = new string[2][];
[STAThread]
static void Main(string[] args)
{
months[0] = new string[12]
{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep",
"Oct", "Nov", "Dec"};
months[1] = new string[12]
{"31", "28", "31", "30", "31", "30", "31", "31", "30",
"31", "30", "31" };
for(int i=0; i<12; i++)
{
System.Console.WriteLine(months[0][i] + " has " + months[1][i] + " days.");
}
System.Console.ReadLine();
}
}
}
Passing Arrays as Parameters
Passing arrays as parameters can be accomplished
in the same manner as all
out
and
ref
parameters.
That is, all
out
parameters do not have to be
Listing 5.4. Passing Arrays as Parameters
using System;
namespace PassingArraysAsParameters
{
class ArraysAsParameters
{
static public void InitializeArray(out string[][] months)
{
months = new string[2][];
}
static public void ModifyArray(ref string[][] months)
{
months[0] =
new string[12] { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
months[1] =
new string[12] { "31", "28", "31", "30", "31", "30", "31",
"31", "30", "31", "30", "31" };
}
[STAThread]
static void Main(string[] args)
{
string[][] months;
InitializeArray(out months);
ModifyArray(ref months);
for(int i=0; i<12; i++)
{
System.Console.WriteLine(months[0][i] + " has " + months[1][i] + " days.");
}
System.Console.ReadLine();
}
}
}
|
|
|
|