次の方法で共有


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

指定した範囲の要素の順序を反転させます。

Overloads Public Overridable Sub Reverse( _
   ByVal index As Integer, _   ByVal count As Integer _)
[C#]
public virtual void Reverse(intindex,intcount);
[C++]
public: virtual void Reverse(intindex,intcount);
[JScript]
public function Reverse(
   index : int,count : int);

パラメータ

  • index
    反転させる範囲の開始位置を示す 0 から始まるインデックス。
  • count
    反転させる範囲内にある要素の数。

例外

例外の種類 条件
ArgumentOutOfRangeException index が 0 未満です。

または

count が 0 未満です。

ArgumentException index および count が ArrayList 内の要素の有効範囲を示していません。
NotSupportedException ArrayList が読み取り専用です。

解説

このメソッドは、 Array.Reverse を使用して要素の順序を反転させます。 ArrayList [i] (i は範囲内の任意のインデックス番号) の位置にある要素は、 ArrayList [j] (j は index + index + count - i - 1 に等しい値) に移動します。

使用例

ArrayList のセクション内の値の並べ替え順序を反転させる方法の例を次に示します。

 
Imports System
Imports System.Collections
Imports Microsoft.VisualBasic

Public Class SamplesArrayList    
    
    Public Shared Sub Main()
        
        ' Creates and initializes a new ArrayList.
        Dim myAL As New ArrayList()
        myAL.Add("The")
        myAL.Add("QUICK")
        myAL.Add("BROWN")
        myAL.Add("FOX")
        myAL.Add("jumps")
        myAL.Add("over")
        myAL.Add("the")
        myAL.Add("lazy")
        myAL.Add("dog")
        
        ' Displays the values of the ArrayList.
        Console.WriteLine("The ArrayList initially contains the " _
           + "following values:")
        PrintIndexAndValues(myAL)
        
        ' Reverses the sort order of the values of the ArrayList.
        myAL.Reverse(1, 3)
        
        ' Displays the values of the ArrayList.
        Console.WriteLine("After reversing:")
        PrintIndexAndValues(myAL)
    End Sub 'Main
    
    
    Public Shared Sub PrintIndexAndValues(myList As IEnumerable)
        Dim i As Integer = 0
        Dim myEnumerator As System.Collections.IEnumerator = _
           myList.GetEnumerator()
        While myEnumerator.MoveNext()
            Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
               + "{1}", i, myEnumerator.Current)
            i = i + 1
        End While
        Console.WriteLine()
    End Sub
End Class

' This code produces the following output.
' 
' The ArrayList 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;
using System.Collections;
public class SamplesArrayList  {

   public static void Main()  {

      // Creates and initializes a new ArrayList.
      ArrayList myAL = new ArrayList();
      myAL.Add( "The" );
      myAL.Add( "QUICK" );
      myAL.Add( "BROWN" );
      myAL.Add( "FOX" );
      myAL.Add( "jumps" );
      myAL.Add( "over" );
      myAL.Add( "the" );
      myAL.Add( "lazy" );
      myAL.Add( "dog" );

      // Displays the values of the ArrayList.
      Console.WriteLine( "The ArrayList initially contains the following values:" );
      PrintIndexAndValues( myAL );

      // Reverses the sort order of the values of the ArrayList.
      myAL.Reverse( 1, 3 );

      // Displays the values of the ArrayList.
      Console.WriteLine( "After reversing:" );
      PrintIndexAndValues( myAL );
   }

   public static void PrintIndexAndValues( IEnumerable myList )  {
      int i = 0;
      System.Collections.IEnumerator myEnumerator = myList.GetEnumerator();
      while ( myEnumerator.MoveNext() )
         Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
      Console.WriteLine();
   }
}
/* 
This code produces the following output.

The ArrayList 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;
using namespace System::Collections;

void PrintIndexAndValues( IEnumerable* myList );
 
int main()  {
 
       // Creates and initializes a new ArrayList instance.
       ArrayList* myAL = new ArrayList();
       myAL->Add( S"The" );
       myAL->Add( S"QUICK" );
       myAL->Add( S"BROWN" );
       myAL->Add( S"FOX" );
       myAL->Add( S"jumped" );
       myAL->Add( S"over" );
       myAL->Add( S"the" );
       myAL->Add( S"lazy" );
       myAL->Add( S"dog" );
 
       // Displays the values of the ArrayList.
       Console::WriteLine( "The ArrayList instance initially contains the following values:" );
       PrintIndexAndValues( myAL );
 
       // Reverses the sort order of the values of the ArrayList.
       myAL->Reverse( 1, 3 );
 
       // Displays the values of the ArrayList.
       Console::WriteLine( "After reversing:" );
       PrintIndexAndValues( myAL );
    }
 
void PrintIndexAndValues( IEnumerable* myList )  {
       int i = 0;
       System::Collections::IEnumerator* myEnumerator = myList->GetEnumerator();
       while ( myEnumerator->MoveNext() )
          Console::WriteLine( "\t[{0}]:\t{1}", __box(i++), myEnumerator->Current );
       Console::WriteLine();
    }

 /* 
 This code produces the following output.
 
The ArrayList 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;
import System.Collections;

// Creates and initializes a new ArrayList.
var myAL : ArrayList = new ArrayList();
myAL.Add( "The" );
myAL.Add( "QUICK" );
myAL.Add( "BROWN" );
myAL.Add( "FOX" );
myAL.Add( "jumped" );
myAL.Add( "over" );
myAL.Add( "the" );
myAL.Add( "lazy" );
myAL.Add( "dog" );

// Displays the values of the ArrayList.
Console.WriteLine( "The ArrayList initially contains the following values:" );
PrintIndexAndValues( myAL );

// Reverses the sort order of the values of the ArrayList.
myAL.Reverse( 1, 3 );

// Displays the values of the ArrayList.
Console.WriteLine( "After reversing:" );
PrintIndexAndValues( myAL );
 
function PrintIndexAndValues( myList : IEnumerable )  {
   var i : int = 0;
   var myEnumerator : System.Collections.IEnumerator  = myList.GetEnumerator();
   while ( myEnumerator.MoveNext() )
      Console.WriteLine( "\t[{0}]:\t{1}", i++, myEnumerator.Current );
   Console.WriteLine();
}
 /* 
 This code produces the following output.
 
 The ArrayList 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

参照

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