System.Array.Reverse 方法 (Array)

方法描述

反转整个一维 Array 中元素的顺序。

语法定义(C# System.Array.Reverse 方法 (Array) 的用法)

public static void Reverse(
	Array array
)

参数/返回值

参数值/返回值 参数类型/返回类型 参数描述/返回描述
array System-Array 要反转的一维 Array。
返回值 void

提示和注释

调用此方法之后,myArray[i](其中 i 为数组中的任意一个索引)处的元素移到 myArray[j](其中 j 等于 (myArray.Length + myArray.GetLowerBound(0)) - (i - myArray.GetLowerBound(0)) - 1)。

此方法的运算复杂度为 O(n),其中 n 是 array 的 Length。

System.Array.Reverse 方法 (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]:    dog
After reversing:
    [0]:    dog
    [1]:    lazy
    [2]:    the
    [3]:    over
    [4]:    jumps
    [5]:    fox
    [6]:    brown
    [7]:    quick
    [8]:    The
*/

异常

异常 异常描述
ArgumentNullException array 为 null。
RankException array 是多维的。

命名空间

namespace: System

程序集: mscorlib(在 mscorlib.dll 中)

版本信息

.NET Framework 受以下版本支持:4、3.5、3.0、2.0、1.1、1.0 .NET Framework Client Profile 受以下版本支持:4、3.5 SP1 受以下版本支持:

适用平台

Windows 7, Windows Vista SP1 或更高版本, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008(不支持服务器核心), Windows Server 2008 R2(支持 SP1 或更高版本的服务器核心), Windows Server 2003 SP2 .NET Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。