次の方法で共有


ArrayList.Reverse メソッド

定義

ArrayList またはその一部の要素の順序を反転させます。

オーバーロード

Reverse()

ArrayList 全体の要素の順序を反転させます。

Reverse(Int32, Int32)

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

Reverse()

ソース:
ArrayList.cs
ソース:
ArrayList.cs
ソース:
ArrayList.cs

ArrayList 全体の要素の順序を反転させます。

public:
 virtual void Reverse();
public virtual void Reverse ();
abstract member Reverse : unit -> unit
override this.Reverse : unit -> unit
Public Overridable Sub Reverse ()

例外

ArrayList は読み取り専用です。

次のコード例は、 の値の並べ替え順序を逆にする方法を ArrayList示しています。

using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList );
int main()
{
   
   // Creates and initializes a new ArrayList.
   ArrayList^ myAL = gcnew 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:" );
   PrintValues( myAL );
   
   // Reverses the sort order of the values of the ArrayList.
   myAL->Reverse();
   
   // Displays the values of the ArrayList.
   Console::WriteLine( "After reversing:" );
   PrintValues( myAL );
}

void PrintValues( IEnumerable^ myList )
{
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::WriteLine( "   {0}", obj );
   }

   Console::WriteLine();
}

/* 
 This code produces the following output.
 
 The ArrayList initially contains the following values:
    The
    quick
    brown
    fox
    jumps
    over
    the
    lazy
    dog

 After reversing:
    dog
    lazy
    the
    over
    jumps
    fox
    brown
    quick
    The
 */
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:" );
      PrintValues( myAL );

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

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

   public static void PrintValues( IEnumerable myList )  {
      foreach ( Object obj in myList )
         Console.WriteLine( "   {0}", obj );
      Console.WriteLine();
   }
}


/*
This code produces the following output.

The ArrayList initially contains the following values:
   The
   quick
   brown
   fox
   jumps
   over
   the
   lazy
   dog

After reversing:
   dog
   lazy
   the
   over
   jumps
   fox
   brown
   quick
   The
*/
Imports System.Collections

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:")
        PrintValues(myAL)
        
        ' Reverses the sort order of the values of the ArrayList.
        myAL.Reverse()
        
        ' Displays the values of the ArrayList.
        Console.WriteLine("After reversing:")
        PrintValues(myAL)
    End Sub
    
    Public Shared Sub PrintValues(myList As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myList
            Console.WriteLine("   {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub

End Class


' This code produces the following output.
' 
' The ArrayList initially contains the following values:
'    The
'    quick
'    brown
'    fox
'    jumps
'    over
'    the
'    lazy
'    dog
'
' After reversing:
'    dog
'    lazy
'    the
'    over
'    jumps
'    fox
'    brown
'    quick
'    The

注釈

このメソッドでは、 を使用Array.Reverseして要素の順序を逆にし、i が範囲内の任意のインデックスである [i] の要素ArrayListが [j] にArrayList移動します。ここで、j は - i - 1 です + indexindex + count

このメソッドは 操作です O(n) 。ここで n 、 は Countです。

適用対象

Reverse(Int32, Int32)

ソース:
ArrayList.cs
ソース:
ArrayList.cs
ソース:
ArrayList.cs

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

public:
 virtual void Reverse(int index, int count);
public virtual void Reverse (int index, int count);
abstract member Reverse : int * int -> unit
override this.Reverse : int * int -> unit
Public Overridable Sub Reverse (index As Integer, count As Integer)

パラメーター

index
Int32

反転させる範囲の開始位置を示す 0 から始まるインデックス。

count
Int32

反転させる範囲内にある要素の数。

例外

index が 0 未満です。

または

count が 0 未満です。

index および countArrayList において要素の有効な範囲を表していません。

ArrayList は読み取り専用です。

次のコード例は、 内の要素の範囲内の値の並べ替え順序を逆にする方法を ArrayList示しています。

using namespace System;
using namespace System::Collections;
void PrintValues( IEnumerable^ myList );
int main()
{
   
   // Creates and initializes a new ArrayList.
   ArrayList^ myAL = gcnew 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:" );
   PrintValues( 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:" );
   PrintValues( myAL );
}

void PrintValues( IEnumerable^ myList )
{
   IEnumerator^ myEnum = myList->GetEnumerator();
   while ( myEnum->MoveNext() )
   {
      Object^ obj = safe_cast<Object^>(myEnum->Current);
      Console::WriteLine( "   {0}", obj );
   }

   Console::WriteLine();
}

/* 
 This code produces the following output.
 
 The ArrayList initially contains the following values:
    The
    QUICK
    BROWN
    FOX
    jumps
    over
    the
    lazy
    dog

 After reversing:
    The
    FOX
    BROWN
    QUICK
    jumps
    over
    the
    lazy
    dog

 */
using System;
using System.Collections;
public class SamplesArrayList1  {

   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:" );
      PrintValues( 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:" );
      PrintValues( myAL );
   }

   public static void PrintValues( IEnumerable myList )  {
      foreach ( Object obj in myList )
         Console.WriteLine( "   {0}", obj );
      Console.WriteLine();
   }
}


/*
This code produces the following output.

The ArrayList initially contains the following values:
   The
   QUICK
   BROWN
   FOX
   jumps
   over
   the
   lazy
   dog

After reversing:
   The
   FOX
   BROWN
   QUICK
   jumps
   over
   the
   lazy
   dog

*/
Imports System.Collections

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:")
        PrintValues(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:")
        PrintValues(myAL)

    End Sub

    Public Shared Sub PrintValues(myList As IEnumerable)
        Dim obj As [Object]
        For Each obj In  myList
            Console.WriteLine("   {0}", obj)
        Next obj
        Console.WriteLine()
    End Sub

End Class


' This code produces the following output.
' 
' The ArrayList initially contains the following values:
'    The
'    QUICK
'    BROWN
'    FOX
'    jumps
'    over
'    the
'    lazy
'    dog
'
' After reversing:
'    The
'    FOX
'    BROWN
'    QUICK
'    jumps
'    over
'    the
'    lazy
'    dog

注釈

このメソッドでは、 を使用Array.Reverseして要素の順序を逆にし、i が範囲内の任意のインデックスである [i] の要素ArrayListが [j] にArrayList移動します。ここで、j は - i - 1 です + indexindex + count

このメソッドは 操作です O(n) 。ここで n 、 は countです。

適用対象