The Built-In Methods of System.Array

   


The Built-In Methods of System.Array

Recall how the System.String class provides any object of type string with a large number of methods and properties. The System.Array class also has numerous built-in methods and properties, providing useful functionality when working with arrays. You have already met a couple of these in the form of the Length property and the GetLength method. As usual, we apply the dot . operator to reach this built-in functionality

 accountBalances.Length   //Returns the length of accountBalances 

which calls the Length property of accountBalances.

The complete collection of methods and properties is listed in the .NET Frameworks Reference, along with their descriptions. Table 11.3 merely presents a few of the most commonly used methods.

Some of the examples in Figure 11.3 contain specific arrays that are described after the table.

Table 11.3. Selected Methods and Properties from System.Array
Method / Property Explanation Example
System.Array.IndexOf  graphics/ccc.gif(<Array_identifier>, <Value>) 

Notes:

<Value> must be identical to the base type of <Array_identifier>.

IndexOf is overloaded. Two other overloaded versions of IndexOf are shown next.

Returns the index of the first array element holding a value equal to <Value>. -1 is returned if no matching values were found.
System.Array.IndexOf(myArray,4) 

Returns the value 2.

System.Array.IndexOf(myArray,32) 

Returns the value -1.

System.Array.IndexOf  graphics/ccc.gif(<Array_identifier>,  graphics/ccc.gif<Value>, <Start_Index>) 
Returns the index of the first array element holding a value equal to <Value>. The search will commence at the array element with index <Start_Index> and finish at the end of the collection.
System.Array.IndexOf(myArray,4,3) 

Returns the value 5.

System.Array.IndexOf  graphics/ccc.gif(myArray,15,5) 

Returns the value -1.

System.Array.IndexOf  graphics/ccc.gif(<Array_identifier>,  graphics/ccc.gif<Value>, <Start_index>,  graphics/ccc.gif<Finish_index>) 
Returns the index of the first array element holding a value equal to <Value>. The search will commence at the array element with index <Start_index> and end at <Finish_index>.
System.Array.IndexOf  graphics/ccc.gif(myArray,2,1,4) 

Returns the value 3.

System.Array.IndexOf  graphics/ccc.gif(<Array_identifier>, <Value>, graphics/ccc.gif<Start_index>, <Finish_index>) 
Returns the index of the last array element holding a value equal to<Value>. The search will commence at the array element with index <Start_Index> and move backwards towards the <Finish_index>.
System.Array.LastIndexOf  graphics/ccc.gif(myArray,2,5,1) 

Returns the value 3.

System.Array.LastIndexOf  graphics/ccc.gif(myArray,2,5,4) 

Returns the value -1.

System.Array.Clear  graphics/ccc.gif(<Array_identifier>, <Start_index>, <Length>) 
Sets <Length> number of array elements to their default initialization value, beginning at <Start_index>.
System.Array.Clear(myArray,1,4) 

Sets the elements of myArray to:

{2, 0, 0, 0, 0, 4, 2}

System.Array.Sort  graphics/ccc.gif(<Array_identifier>) 
Sorts the elements of the array in ascending order.
System.Array.Sort(myArray) 

Sets the elements of myArray to:

{1, 2, 2, 2, 4, 4, 15}

System.Array.Reverse  graphics/ccc.gif(<Array_identifier>) 
Reverses the order of the array elements of the given array.
System.Array.Reverse(myArray) 

Sets the elements of myArray to:

{2, 4, 15, 2, 4, 1, 2}

System.Array.Reverse  graphics/ccc.gif(<Array_identifier>,  graphics/ccc.gif<Start_index>,  graphics/ccc.gif<Finish_index>) 
Reverses the order of the elements of an array in a given interval beginning at <Start_index> and finishing at <Finish_index>.
System.Array.Reverse  graphics/ccc.gif(myArray,1,4) 

Sets the elements of myArray to:

{2, 15, 2, 4, 1, 4, 2}

System.Array.Copy  graphics/ccc.gif(<Array_identifier1>,  graphics/ccc.gif<Array_identifier2>, <Length>) 
Copies <Length> amount of elements from <Array_identifier1> to <Array_identifier2> starting at index 0.
System.Array.Copy  graphics/ccc.gif(myArray,myCopy,4) 

Leaves myArray untouched and sets the elements of myCopy to:

{2, 1, 4, 2, 0, 0, 0}

System.Array.Copy  graphics/ccc.gif(<Array_identifier1>,  graphics/ccc.gif<Start_index1>,  graphics/ccc.gif<Array_identifier2>,  graphics/ccc.gif<Start_index2>, <Length>) 
Copies <Length> amount of elements from index <Start_index1> of <Array_identifier1> to <Array_identifier2> beginning at <Start_index2>.
System.Array.Copy  graphics/ccc.gif(myArray,1,myCopy,2,3) 

Leaves myArray untouched and sets the elements of myCopy to:

{0, 0, 1, 4, 2, 0, 0}

<Array_identifier1> =  graphics/ccc.gif(<Array_Type>)  graphics/ccc.gif<Array_identifier2>.Clone() 
Creates a shallow copy of the array object referenced by <Array_identifier2> and assigns its reference to <Array_identifier1>.
myClone = (int[])myArray.Clone(); 

Leaves myArray untouched and assigns a reference to a shallow copy of the object referenced by myArray.

System.Array.LastIndexOf  graphics/ccc.gif(<Array_identifier>,<Value>) 

Note: This method is overloaded. Another overloaded version is shown next.

Returns the index of the last array element holding a value equal to <Value>. -1 is returned if no matching values were found.
System.Array.LastIndexOf  graphics/ccc.gif(myArray,2) 

Returns the value 6.

System.Array.LastIndexOf  graphics/ccc.gif(<Array_identifier>, <Value>,  graphics/ccc.gif<Start_Index>) 
Returns the index of the last array element holding a value equal to <Value>. The search will commence at the array element with index <Start_Index> and move backwards towards the beginning of the array.
System.Array.LastIndexOf  graphics/ccc.gif(myArray,2,2) 

Returns the value 0

Notes:

  • myArray used in the examples of the table is declared and defined as

     int [] myArray = new int[7] {2, 1, 4, 2, 15, 4, 2} ; 
  • myCopy used in a few examples is an array declared and defined as

     int[] myCopy = new int[7]; 
  • myClone is used in the cloning example and is declared as

     int[] myClone; 
  • Programs including the line

     using System; 

    can omit the reference to System when calling the static methods of System.Array, so, for example, instead of writing

     System.Array.IndexOf(<Array_identifier>, <Value>); 

    You can simply write

     Array.IndexOf(<Array_identifier>, <Value>); 

   


C# Primer Plus
C Primer Plus (5th Edition)
ISBN: 0672326965
EAN: 2147483647
Year: 2000
Pages: 286
Authors: Stephen Prata

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