Navigating through the Array


There are two main ways of navigating through the elements of an array. Arrays let you address elements using an index number. Indices begin at zero and go up to the dimensions of the array, minus 1. The class System.Array also implements the IEnumerable interface. Any class that implements IEnumerable enables you to navigate through its members using the C# foreach command.

To navigate through the array using the index number:

  1. Type for.

  2. Type an open parenthesis ( .

  3. Type the name of the variable to serve as the index, then type an equal sign = and the number zero, followed by a semicolon ; , for example: int index = 0; .

  4. Type the name of the index variable followed by a less-than sign < followed by the name of the array plus .Length , plus a semicolon ; , for example: index < accts.Length; .

  5. Type the name of the index variable plus the ++ operator, for example: index++ .

  6. Type a close parenthesis ) .

  7. Type an open curly bracket { .

  8. Address the element of the array as follows : accts[index] , where accts is the name of the array and index is the variable you used in the for declaration.

  9. Type a close curly bracket ( Figure 9.18 ).

    Figure 9.18 Array indexes are zero based. This is a standard that .NET languages have. That means that valid indexes are zero to Length -1.
     class Book {    string _title;    public string Read(string title)    {       _title = title;       return "Once upon a time";    } } void Task() {    Book[] library = new Book[55];  for (int count = 0; count <   library.Length; count++)   {   library[count] = new Book();   library[count].Read("Book" +   count);   }  } 

To navigate through the array using the foreach command:

  1. Type foreach .

  2. Type an open parenthesis ( .

  3. Type the type of the element stored in the array, for example: Checking .

  4. Type the name of a variable to hold the current item in the array, for example current .

  5. Type in .

  6. Type the name of the array, for example: accts .

  7. Type a close parenthesis ) .

  8. Type an open curly bracket { .

  9. Use the variable current in some way, for example: current.balance += 20; .

  10. Type a close curly bracket } ( Figure 9.19 ).

    Figure 9.19 foreach makes it easier to navigate through the array. It even works with multidimensional arrays.
     class Book {    string _title;    public string Read(string title)    {       _title = title;       return "Once upon a time";    }    public void CheckOut()    {    } } void Task() {    Book[] library = new Book[55];    for (int count = 0;    count < library.Length; count++)    {       library[count] = new Book();       library[count].Read("Book" +       count);    }  foreach(Book singlebook in library)   {   singlebook.CheckOut();   }  } 

graphics/tick.gif Tips

  • In some ways it's easier to use the foreach notation (less typing). However, foreach has a limitationthe current item is read-only. This means that if the array is an array of valuetypes then you can't set the value of each element. If the array is an array of reference types, you can't set the current item equal to a new object ( Figure 9.20 ). But if the element is already set to a new object, the fields in the object can be changed as you can see in Figure 9.21 .

    Figure 9.20 The variable that stores the current element in foreach is read-only. That doesn't mean you can't call a method of the object or even set a field in the object. It just means the variable itself can't be replaced with another object.
     void Task() {    Book[] library = new Book[55];    foreach(Book singlebook in library)    {  //*** this line results in a   //*** compiler error   singlebook = new Book();  singlebook.CheckOut();    } } 
    Figure 9.21 As you can see, it's perfectly legal to set the fields in an object if it already has been set but we can't use foreach, for example, to initialize the array (create objects for each element).
     class Book {    public string Title; } void Task() {    Book[] library = new Book[55];    library[3] = new Book();    int count=1;    foreach(Book singlebook in library)    {       if (singlebook != null)       {  singlebook.Title =   "Harry, Galactic Investigator, Part"   + count;  count++;       }    } } 
  • The Length property is a property available to all array types. It gives you the total number of elements in the array. When using the index, the valid index values are through Length -1 . The continuation test in the for loop above is written as index < Length , in other words, loop while index is less than the Length or stop when index >= Length . That means the last index value will be Length -1 .




C#
C# & VB.NET Conversion Pocket Reference
ISBN: 0596003196
EAN: 2147483647
Year: 2003
Pages: 198
Authors: Jose Mojica

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