Microsoft Visual C#.NET 2003 Kick Start
We're already familiar with arrays; they're those indexed collections of objects that must be the same type. Arrays are based on the System.Array class, but you don't use this class directly to create arrays ( System.Array is an abstract class); as we've seen, you use C# language syntax like this to create an array:
int[] intArray = new int[4] {1, 2, 3, 4}; We have already discussed the fundamentals of handling arraysthey're zero-based collections that are reference types. We've seen the default value of array elements in Chapter 3, "C# Object-Oriented Programming," (Table 3.2). However, we haven't learned about the power that the Array class, which arrays are based on, gives us. You can find the significant public properties of Array objects in Table 6.1. The most commonly used is the Length property, which holds the total number of items in the array. You can likewise find the significant public methods of the Array classsome of which are staticin Table 6.2.
Table 6.1. Significant Public Properties of Array Objects
Table 6.2. Significant Public Methods of Array Objects
The elements you store in arrays are always objects in C#. Even simple data types are objects in C#, of course. Here's an example where we explicitly create our own objects and store them in an array; in this case, we'll store Customer objects in an array, where the Customer class stores the customer's name and has cast operators to and from strings:
public class Customer { private string name; public Customer(string name) { this.name = name; } public static implicit operator string(Customer customer) { return customer.name; } public static implicit operator Customer(string name) { return new Customer(name); } } Because the Customer class has a cast operator from strings to Customer objects, you can initialize a set of Customer objects in an array with a set of strings in curly braces, just as you can for a standard string[] array. You can see the code in ch06_01.cs, where we're filling an array with Customer objects and then looping over the array to display the name of each customer. Listing 6.1 Storing Objects in Arrays (ch06_01.cs)
public class ch06_01 { static void Main() { Customer[] customerArray = new Customer[4] {"Bob", "Tom", "Mary", "Sue"}; System.Console.WriteLine("Here are the customers:"); foreach (Customer customer in customerArray) { System.Console.WriteLine(customer); } } } public class Customer { private string name; public Customer(string name) { this.name = name; } public static implicit operator string(Customer customer) { return customer.name; } public static implicit operator Customer(string name) { return new Customer(name); } } Here's what you see when you run ch06_01:
C:\>ch06_01 Here are the customers: Bob Tom Mary Sue Creating Multidimensional Arrays
Up to now we've used only single-dimension arrays, but C# supports multidimensional arrays as well. You can indicate how many dimensions a multidimensional array has using commas in the brackets following the array type in its declaration. For example, here's how you might create a two-dimensional array of numberRows rows and numberColumns columns :
int[ , ] array = new int[numberRows, numberColumns]; Now you can address the elements in this array as array[ row, column ] . You can see an example in ch06_02.cs, Listing 6.2, where we create a two-dimensional array with two rows and three columns, fill it with random numbers using an object of the System.Random class (the Next method of this class returns an integer with the maximum value you pass to it; in this case, 10), and display those values. Listing 6.2 Creating a Multidimensional Array (ch06_02.cs)
public class ch06_02 { static void Main() { const int numberRows = 2; const int numberColumns = 3; System.Random random = new System.Random(); int[ , ] array = new int[numberRows, numberColumns]; for (int row = 0; row < numberRows; row++) { for (int column = 0; column < numberColumns; column++) { array[row, column] = random.Next(10); } } for (int row = 0; row < numberRows; row++) { for (int column = 0; column < numberColumns; column++) { System.Console.Write("array[{0}, {1}] = {2} ", row, column, array[row, column]); } System.Console.WriteLine(); } } } Here's what you might see when you run ch06_02, where each array element has been filled with a random integer up to a value of 10:
C:\>ch06_02 array[0, 0] = 3 array[0, 1] = 6 array[0, 2] = 6 array[1, 0] = 3 array[1, 1] = 4 array[1, 2] = 8 You can also initialize multidimensional arrays in much the same way as you can with a single-dimensional array. For example, to store the integers we've just seen in the previous example in array, you can use code like the following. You just enclose the multiple dimensions of the initializers in successive levels of curly braces:
public class ch06_02 { static void Main() { const int numberRows = 2; const int numberColumns = 3; System.Random random = new System.Random(); int[ , ] array = { {3, 6, 6}, {3, 4, 8} }; for (int row = 0; row < numberRows; row++) { for (int column = 0; column < numberColumns; column++) { System.Console.Write("array[{0}, {1}] = {2} ", row, column, array[row, column]); } System.Console.WriteLine(); } } } This code gives you the same results as ch06_01.cs. Creating Arrays of Arrays: Jagged Arrays
You can also create arrays of arrays, called jagged arrays because they need not be rectangular. Here's the syntax you use to create an array of arrays. Note the syntax in this case, int[][] :
int[][] array = new int[4][]; This creates an array of four single-dimensional arrays, each of which can be a different length. To create the jagged array, you assign those single-dimensional arrays to the various elements of the jagged array like this:
int[][] array = new int[4][]; array[0] = new int[5]; array[1] = new int[3]; array[2] = new int[5]; array[3] = new int[3]; Now you can use this new jagged array as you can any rectangular array; just remember how many elements are in each row and don't try to access elements outside the bounds of the array. You can see how this works in code in ch06_03.cs, Listing 6.3, where we use two foreach loops to loop over each array in array and display every value in the entire jagged array. Listing 6.3 Creating a Jagged Array (ch06_03.cs)
public class ch06_03 { static void Main() { int[][] array = new int[4][]; array[0] = new int[5]; array[1] = new int[3]; array[2] = new int[5]; array[3] = new int[3]; array[0][2] = 1; array[0][4] = 3; array[1][1] = 5; array[1][2] = 3; array[2][1] = 9; array[2][2] = 2; array[2][4] = 1; array[3][0] = 7; array[3][1] = 4; array[3][2] = 9; foreach(int[] a in array) { foreach(int i in a) { System.Console.Write("{0} ", i); } System.Console.WriteLine(); } } } Here's what the jagged array looks like when you display its elements:
C:\>ch06_03 0 0 1 0 3 0 5 3 0 9 2 0 1 7 4 9 Sorting and Reversing Arrays
The System.Array class comes with a number of good utility methods built into it; for example, the Reverse method reverses the order of the elements in an array, and the Sort method sorts those elements. You can see both methods at work in ch06_04.cs, where we reverse and sort an array with the elements "Now" , "is" , "the" , and "time" . Listing 6.4 Sorting and Reversing an Array (ch06_04.cs)
public class ch06_04 { static void Main() { string[] array = {"Now", "is", "the", "time"}; System.Console.WriteLine("The array:"); foreach (string element in array) { System.Console.Write("{0} ", element); } System.Array.Reverse(array); System.Console.WriteLine(); System.Console.WriteLine("The reversed array:"); foreach (string element in array) { System.Console.Write("{0} ", element); } System.Array.Sort(array); System.Console.WriteLine(); System.Console.WriteLine("The sorted array:"); foreach (string element in array) { System.Console.Write("{0} ", element); } } } Here's what you see when you run ch06_04. As you can see, the array was indeed reversed and sorted as desired:
C:\>ch06_04 The array: Now is the time The reversed array: time the is Now The sorted array: is Now the time Using Bit Arrays
Bit arrays are a special kind of array, supported by the BitArray class. The elements in these arrays are treated as bit values and you can use logical methods like And and Or on them. Bit arrays can be useful when you need to perform operations on the individual bits in a value. For example, if you need to set the seventeenth bit in the octal value 777356, what would the new value be? You can pass integer values to the BitArray constructor and use the Set and Get methods to set and get the values of individual bits. You can also access the individual bits in a bit array as you can any element in an array, like this: bitArray[5] = true . You can see the significant public properties of BitArray objects in Table 6.3 and the significant public methods in Table 6.4. Table 6.3. Significant Public Properties of BitArray Objects
Table 6.4. Significant Public Methods of BitArray Objects
For example, we'll create two bit arrays, initializing the bits in them with bool values, and then And them together and display the result. You can see the code in ch06_05.cs, Listing 6.5. Note that BitArray , like the other collections we'll see in this chapter, is in the System.Collections namespace (excluding Array , which is in the System namespace). Listing 6.5 Creating a Bit Array (ch06_05.cs)
using System.Collections; public class ch06_05 { public static void Main() { bool[] boolArray = new bool[5] {true, false, true, true, false}; BitArray bitArray1 = new BitArray(boolArray); boolArray = new bool[5] {true, true, false, true, false}; BitArray bitArray2 = new BitArray(boolArray); BitArray bitArray3; System.Console.WriteLine("bitArray1:" ); foreach(bool element in bitArray1) { System.Console.Write("{0} ", element); } System.Console.WriteLine(); System.Console.WriteLine("bitArray2:"); foreach(bool element in bitArray2) { System.Console.Write("{0} ", element); } bitArray3 = bitArray1.And(bitArray2); System.Console.WriteLine(); System.Console.WriteLine("bitArray1.And(bitArray2):"); foreach(bool element in bitArray3) { System.Console.Write("{0} ", element); } } } When you run ch06_05, you see our two bit arrays expressed as arrays of Boolean values, followed by the result of And ing them together:
C:\>ch06_05 bitArray1: True False True True False bitArray2: True True False True False bitArray1.And(bitArray2): True False False True False |