精品收藏---集合之Array.Reverse 方法的技巧

来源:互联网 发布:网件路由器访客网络 编辑:程序博客网 时间:2024/05/22 01:55

语法


 public static void Reverse(
Array array
)

参数

array
类型:System.Array
要反转的一维 Array

示例


下面的代码示例说明如何反转 Array 中的值的排序。

using System;public class SamplesArray  {   public static void Main()  {      // Creates and initializes a new Array.      Array myArray=Array.CreateInstance( typeof(String), 9 );      myArray.SetValue( "The", 0 );      myArray.SetValue( "quick", 1 );      myArray.SetValue( "brown", 2 );      myArray.SetValue( "fox", 3 );      myArray.SetValue( "jumps", 4 );      myArray.SetValue( "over", 5 );      myArray.SetValue( "the", 6 );      myArray.SetValue( "lazy", 7 );      myArray.SetValue( "dog", 8 );      // Displays the values of the Array.      Console.WriteLine( "The Array initially contains the following values:" );      PrintIndexAndValues( myArray );      // Reverses the sort of the values of the Array.      Array.Reverse( myArray );      // Displays the values of the Array.      Console.WriteLine( "After reversing:" );      PrintIndexAndValues( myArray );   }   public static void PrintIndexAndValues( Array myArray )  {      for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )         Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );   }}/* This code produces the following output.The Array initially contains the following values:    [0]:    The    [1]:    quick    [2]:    brown    [3]:    fox    [4]:    jumps    [5]:    over    [6]:    the    [7]:    lazy    [8]:    dogAfter reversing:    [0]:    dog    [1]:    lazy    [2]:    the    [3]:    over    [4]:    jumps    [5]:    fox    [6]:    brown    [7]:    quick    [8]:    The*/ 

 

语法


 public static void Reverse(
Array array,
int index,
int length
)

参数

array
类型:System.Array
要反转的一维Array
index
类型:System.Int32
要反转的部分的起始索引。
length
类型:System.Int32
要反转的部分中的元素数。
示例

下面的代码示例显示如何反转 Array 中某部分元素的值序。

using System;public class SamplesArray  {   public static void Main()  {      // Creates and initializes a new Array.      Array myArray=Array.CreateInstance( typeof(String), 9 );      myArray.SetValue( "The", 0 );      myArray.SetValue( "QUICK", 1 );      myArray.SetValue( "BROWN", 2 );      myArray.SetValue( "FOX", 3 );      myArray.SetValue( "jumps", 4 );      myArray.SetValue( "over", 5 );      myArray.SetValue( "the", 6 );      myArray.SetValue( "lazy", 7 );      myArray.SetValue( "dog", 8 );      // Displays the values of the Array.      Console.WriteLine( "The Array initially contains the following values:" );      PrintIndexAndValues( myArray );      // Reverses the sort of the values of the Array.      Array.Reverse( myArray, 1, 3 );      // Displays the values of the Array.      Console.WriteLine( "After reversing:" );      PrintIndexAndValues( myArray );   }   public static void PrintIndexAndValues( Array myArray )  {      for ( int i = myArray.GetLowerBound(0); i <= myArray.GetUpperBound(0); i++ )         Console.WriteLine( "\t[{0}]:\t{1}", i, myArray.GetValue( i ) );   }}/* This code produces the following output.The Array initially contains the following values:    [0]:    The    [1]:    QUICK    [2]:    BROWN    [3]:    FOX    [4]:    jumps    [5]:    over    [6]:    the    [7]:    lazy    [8]:    dogAfter reversing:    [0]:    The    [1]:    FOX    [2]:    BROWN    [3]:    QUICK    [4]:    jumps    [5]:    over    [6]:    the    [7]:    lazy    [8]:    dog*/