次の方法で共有


ArrayList.Reverse メソッド ()

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

Overloads Public Overridable Sub Reverse()
[C#]
public virtual void Reverse();
[C++]
public: virtual void Reverse();
[JScript]
public function Reverse();

例外

例外の種類 条件
NotSupportedException ArrayList が読み取り専用です。

使用例

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()
        
        ' Displays the values of the ArrayList.
        Console.WriteLine("After reversing:")
        PrintIndexAndValues(myAL)
    End Sub
    
    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]:    dog
'     [1]:    lazy
'     [2]:    the
'     [3]:    over
'     [4]:    jumps
'     [5]:    fox
'     [6]:    brown
'     [7]:    quick
'     [8]:    The 

[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();

      // 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]:    dog
    [1]:    lazy
    [2]:    the
    [3]:    over
    [4]:    jumps
    [5]:    fox
    [6]:    brown
    [7]:    quick
    [8]:    The
*/ 

[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();
 
       // 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]:    dog
        [1]:    lazy
        [2]:    the
        [3]:    over
        [4]:    jumped
        [5]:    fox
        [6]:    brown
        [7]:    quick
        [8]:    The

 */ 

[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();

// 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]:    dog
     [1]:    lazy
     [2]:    the
     [3]:    over
     [4]:    jumped
     [5]:    fox
     [6]:    brown
     [7]:    quick
     [8]:    The
 */ 

必要条件

プラットフォーム: 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 オーバーロードの一覧