次の方法で共有


Array.Reverse メソッド (Array, Int32, Int32)

1 次元 Array の範囲内の要素のシーケンスを反転させます。

Overloads Public Shared Sub Reverse( _
   ByVal array As Array, _   ByVal index As Integer, _   ByVal length As Integer _)
[C#]
public static void Reverse(Arrayarray,intindex,intlength);
[C++]
public: static void Reverse(Array* array,intindex,intlength);
[JScript]
public static function Reverse(
   array : Array,index : int,length : int);

パラメータ

  • array
    反転させる 1 次元の Array
  • index
    反転させる範囲の先頭位置を示すインデックス。
  • length
    反転対象の範囲内にある要素の数。

例外

例外の種類 条件
ArgumentNullException array が null 参照 (Visual Basic では Nothing) です。
RankException array が多次元です。
ArgumentOutOfRangeException index が array の下限より小さい値です。

または

length が 0 未満です。

ArgumentException index および length が array 内の有効な範囲を指定していません。

解説

このメソッドが呼び出された後、 myArray[i] (i は配列内の任意のインデックス) の位置にある要素が、 myArray[j] (j( myArray.Length + myArray.GetLowerBound(0) ) - ( i - myArray.GetLowerBound(0) ) - 1 に等しい) の位置に移動します。

使用例

Array の範囲内の値の並べ替え順序を反転させる方法を次のコード例に示します。

 
Imports System
Imports Microsoft.VisualBasic

Public Class SamplesArray    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new Array.
        Dim myArray As Array = Array.CreateInstance(GetType(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)
    End Sub    
    
    
    Public Shared Sub PrintIndexAndValues(myArray As Array)
        Dim i As Integer
        For i = myArray.GetLowerBound(0) To myArray.GetUpperBound(0)
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myArray.GetValue(i))
        Next i
    End Sub
End Class

' 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]:    The
'     [1]:    FOX
'     [2]:    BROWN
'     [3]:    QUICK
'     [4]:    jumps
'     [5]:    over
'     [6]:    the
'     [7]:    lazy
'     [8]:    dog 

[C#] 
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]:    dog
After reversing:
    [0]:    The
    [1]:    FOX
    [2]:    BROWN
    [3]:    QUICK
    [4]:    jumps
    [5]:    over
    [6]:    the
    [7]:    lazy
    [8]:    dog
*/ 

[C++] 
#using <mscorlib.dll>
using namespace System;

void PrintIndexAndValues( Array* myArray );
 
void main()  {
 
       // Creates and initializes a new Array instance.
       Array* myArray = Array::CreateInstance( __typeof(String), 9 );
       myArray->SetValue( S"The", 0 );
       myArray->SetValue( S"QUICK", 1 );
       myArray->SetValue( S"BROWN", 2 );
       myArray->SetValue( S"FOX", 3 );
       myArray->SetValue( S"jumped", 4 );
       myArray->SetValue( S"over", 5 );
       myArray->SetValue( S"the", 6 );
       myArray->SetValue( S"lazy", 7 );
       myArray->SetValue( S"dog", 8 );
 
       // Displays the values of the Array.
       Console::WriteLine( "The Array instance 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 );
}
 
 
void PrintIndexAndValues( Array* myArray )  {
    for ( int i = myArray->GetLowerBound(0); i <= myArray->GetUpperBound(0); i++ )
        Console::WriteLine( "\t[{0}]:\t{1}", __box(i), myArray->GetValue( i ) );
}

 /* 
 This code produces the following output.
 
 The Array instance initially contains the following values:
     [0]:    The
     [1]:    QUICK
     [2]:    BROWN
     [3]:    FOX
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 After reversing:
     [0]:    The
     [1]:    FOX
     [2]:    BROWN
     [3]:    QUICK
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 */ 

[JScript] 
import System;

// Creates and initializes a new Array.
var myArray : System.Array= System.Array.CreateInstance( System.String, 9 );
myArray.SetValue( "The", 0 );
myArray.SetValue( "QUICK", 1 );
myArray.SetValue( "BROWN", 2 );
myArray.SetValue( "FOX", 3 );
myArray.SetValue( "jumped", 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.
System.Array.Reverse( myArray, 1, 3 );

// Displays the values of the Array.
Console.WriteLine( "After reversing:" );
PrintIndexAndValues( myArray );
 
 
function PrintIndexAndValues( myArray : System.Array )  {
   for ( var i : int = 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]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 After reversing:
     [0]:    The
     [1]:    FOX
     [2]:    BROWN
     [3]:    QUICK
     [4]:    jumped
     [5]:    over
     [6]:    the
     [7]:    lazy
     [8]:    dog
 */ 

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

Array クラス | Array メンバ | System 名前空間 | Array.Reverse オーバーロードの一覧