Section A.10. Chapter 10: Arrays


A.10. Chapter 10: Arrays

A.10.1. Quiz



Solution to Question 101 .

6. Arrays always begin with index (or offset) zero.



Solution to Question 102 .

Yes. Every array declares the type of objects it will hold. You can undermine the type-safety by creating an array of objects (which will hold anything, because everything derives from objects), but that is not advised.



Solution to Question 103 .

Arrays are reference types and created on the heap.



Solution to Question 104 .

If the elements of the array are of value types, they are created in the allocated memory for the array; otherwise , they are created elsewhere on the heap and a reference is stored in the array.



Solution to Question 105 .

You can explicitly call new or just imply the size of the array. For example, if you have three employee objects named moe, larry, and curley:

 Employee[] myEmpArray = new Employee[3] = { moe, larry, curley }; 

or:

 Employee[] myEmpArray = { moe, larry, curley }; 



Solution to Question 106 .

Allows you to pass in an indefinite number of parameters, all of the same type, which will be treated as an array. You can, if you wish, also pass in an array.



Solution to Question 107 .

A rectangular array is a multidimensional array; a jagged array is an array of arrays.

A.10.2. Exercises



Solution to Exercise 10-1 .

Declare a Dog class with two private members : weight (an int ), and name (a string ). Be sure to add properties to access the members. Then create an array that holds three Dog objects (Milo, 26 pounds; Frisky, 10 pounds ; and Laika, 50 pounds ). Output each dog's name and weight.

 using System; namespace Exercises {     public class Dog     {       public Dog(int theWeight, string theName)       {           this.weight = theWeight;           this.name = theName;       }       public int Weight       {           get           {               return weight;           }           set           {               weight = value;           }       }       public string Name       {           get           {               return name;           }           set           {               name = value;           }       }       private int weight;       private string name;    }    public class Tester    {       public void Run(  )       {          Dog milo = new Dog(26, "Milo");          Dog frisky = new Dog(10, "Frisky");          Dog laika = new Dog(50, "Laika");          Dog[] dogArray = {milo, frisky, laika};          // output array values          foreach (Dog d in dogArray)          {              Console.WriteLine("Dog {0} weighs {1} pounds.", d.Name, d.Weight);          }       }        static void Main(  )        {            Tester t = new Tester(  );            t.Run(  );        }    } } 



Solution to Exercise 10-2 .

Create an array of 10 integers. Populate the array by having the user enter integers at the console (use Console.Readline ). Don't worry about error checking for this exercise. Output the integers sorted from greatest to least.

 using System; namespace Exercises {    public class Tester    {       public void Run(  )       {          int[] intArray = new int[10];          Console.WriteLine("You'll be asked to enter 10 integers");          // enter data into the array          for (int i = 0; i < intArray.Length; i++ )          {              Console.Write("Enter an integer: ");              string theEntry = Console.ReadLine(  );              intArray[i] = Convert.ToInt32(theEntry);          }          // sort and reverse the array          Array.Sort(intArray);          Array.Reverse(intArray);          Console.WriteLine("\nValues:");          foreach (int j in intArray)          {              Console.WriteLine("{0}", j);          }       }        static void Main(  )        {            Tester t = new Tester(  );            t.Run(  );        }    } } 



Solution to Exercise 10-3 .

Extend Exercise 10-1 by creating a two-dimensional array that represents a collection of strings that indicate the awards each dog has won at dog shows. Each dog may have a different number of awards won. Output the contents of the array to check its validity.

 using System; namespace exercise {     public class Dog     {         public Dog(int theWeight, string theName)         {             this.weight = theWeight;             this.name = theName;         }         public int Weight         {             get             {                 return weight;             }             set             {                 weight = value;             }         }         public string Name         {             get             {                 return name;             }             set             {                 name = value;             }         }         private int weight;         private string name;     }     public class Tester     {         public void Run(  )         {             const int rows = 3;             // declare and populate the dogs array             Dog milo = new Dog(26, "Milo");             Dog frisky = new Dog(10, "Frisky");             Dog laika = new Dog(50, "Laika");             Dog[] dogArray = {milo, frisky, laika};             // declare the dogAwards array as 3 rows high             string[][] dogAwardsArray = new string[rows][];             // declare the rows             dogAwardsArray[0] = new string[3];             dogAwardsArray[1] = new string[1];             dogAwardsArray[2] = new string[2];             // Populate the rows             dogAwardsArray[0][0] = "Best in Show";             dogAwardsArray[0][1] = "Best of Breed";             dogAwardsArray[0][2] = "Judge's Cup";             dogAwardsArray[1][0] = "Best Toy Tog";             dogAwardsArray[2][0] = "Best Working Dog";             dogAwardsArray[2][1] = "Best Large Dog";             // Output the contents             for (int i = 0; i < dogAwardsArray.Length; i++)             {                 Console.WriteLine("{0}'s awards: ", dogArray[i].Name);                 for (int j = 0; j < dogAwardsArray[i].Length; j++)                 {                     Console.WriteLine("\t{0}", dogAwardsArray[i][j]);                 }             }         }        static void Main(  )        {            Tester t = new Tester(  );            t.Run(  );        }     } } 



Solution to Exercise 10-4 .

Create a two-dimensional array that represents a chessboard (an 8 x 8 array). Each element in the array should contain either the string "black" or "white," depending on where it is on the board. Create a method that initializes the array with the strings. Then create a method that asks the reader to enter two integers for the coordinates of a square, and returns whether that square is black or white.

 using System; namespace exercises {     public class Tester     {         public void Run(  )         {             const int rows = 8;             const int columns = 8;             // create an 8x8 array             string[,] chessboardArray = new string[rows, columns];             // populate the chessboard array             for (int i = 0; i < rows; i++)             {                 // if row starts with a black square                 if ((i % 2) == 0)                 {                     for (int j = 0; j < columns; j++)                     {                         if ((j % 2) == 0)                         {                             chessboardArray[i,j] = "black";                         }                         else                         {                             chessboardArray[i,j] = "white";                         }                     }                 }                 // else row starts with a white square                 else                 {                     for (int j = 0; j < columns; j++)                     {                         if ((j % 2) == 0)                         {                             chessboardArray[i,j] = "white";                         }                         else                         {                             chessboardArray[i,j] = "black";                         }                     }                 }              }           // ask the user for coordinates to test             Console.Write("Enter the row to test (1 through 8): ");             string rowEntry = Console.ReadLine(  );             int testRow = Convert.ToInt32(rowEntry);             Console.Write("Enter the column to test (1 through 8): ");             string colEntry = Console.ReadLine(  );             int testCol = Convert.ToInt32(colEntry);             // output the value at those coordinates             Console.WriteLine("The square at {0}, {1} is {2}.", testRow,             testCol, chessboardArray[(testRow - 1), (testCol - 1)]);         }         static void Main(  )        {            Tester t = new Tester(  );            t.Run(  );        }     } } 



Learning C# 2005
Learning C# 2005: Get Started with C# 2.0 and .NET Programming (2nd Edition)
ISBN: 0596102097
EAN: 2147483647
Year: 2004
Pages: 250

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