Using Arrays

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.

FOR C++ PROGRAMMERS

C++ uses the Standard Template Library (STL) to support collection classes such as queues and stacks, but C# does not support the STL. That means nearly all the material in this chapter is new for programmers coming to C# from C++.


USING THE SYSTEM.ARRAY CLASS TO CREATE ARRAYS

The Array class is an abstract class in C#, but it turns out that you can create an object from this class using its CreateInstance method: System.Array array = System.Array.CreateInstance(typeof(System.String), 4); . However, you can't use the [] operator on this array, you have to use the GetValue and SetValue methods instead, like this: string value = array.GetValue( index ) or array.SetValue("No worries.", index ) . Overall, you're better off with the standard array syntax.


Table 6.1. Significant Public Properties of Array Objects

PROPERTY

PURPOSE

IsFixedSize

Returns true if the array has a fixed size.

IsReadOnly

Returns true if the array is read-only.

IsSynchronized

Returns true if access to the array is thread-safe.

Length

Returns a 32-bit integer holding the total number of elements in all the dimensions of the array.

LongLength

Returns a 64-bit integer holding the total number of elements in all the dimensions of the array.

Rank

Returns the number of dimensions of the array.

Table 6.2. Significant Public Methods of Array Objects

PROPERTY

PURPOSE

IsFixedSize

Returns true if the array has a fixed size.

static BinarySearch

Searches a one-dimensional sorted array for a value.

static Clear

Clears a range of elements, setting them to zero, to false , or to a null reference, depending on the element type.

static Copy

Copies a section of one array to another.

CopyTo

Copies all the elements of the current one-dimensional array to the given one-dimensional array.

static CreateInstance

Creates a new object of the Array class.

GetEnumerator

Returns an IEnumerator object for the array.

GetLength

Returns a 32-bit integer holding the number of elements in the given dimension of the array.

GetLongLength

Returns a 64-bit integer holding the number of elements in the given dimension of the array.

GetLowerBound

Returns the lower bound of the given dimension.

GetUpperBound

Returns the upper bound of the given dimension.

GetValue

Returns the value of the given element in the current array.

static IndexOf

Returns the index of the first occurrence of a value.

Initialize

Initializes every element of the value-type array.

static LastIndexOf

Returns the index of the last occurrence of a value.

static Reverse

Reverses the order of the elements in a one-dimensional Array.

SetValue

Sets the given element in the current Array to the given value.

static Sort

Sorts the elements in a one-dimensional array.

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

PROPERTY

PURPOSE

Count

Holds the number of elements.

IsReadOnly

true if the bit array is read-only.

Item

Returns or sets the value of the bit at a specific position.

Length

Returns or sets the number of elements.

Table 6.4. Significant Public Methods of BitArray Objects

METHOD

PURPOSE

And

Performs the bitwise AND operation on the elements in the current bit array with the corresponding elements in the given bit array.

CopyTo

Copies the entire bit array to a compatible one-dimensional array.

Get

Returns the value of the bit at a specific position.

GetEnumerator

Returns an enumerator that can iterate through the bit array.

Not

Inverts all the bit values ( true values are changed to false , and false values are changed to true ).

Or

Performs the bitwise OR operation on the elements in the current bit array with the corresponding elements in the given bit array.

Set

Sets the bit at a specific position to the given value.

SetAll

Sets all bits to the given value.

Xor

Performs the bitwise eXclusive OR operation on the elements in the current bit array with the corresponding elements in the given bit array.

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 


Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net