List(T).Reverse メソッド (System.Collections.Generic)

ビューの切り替え:
スクリプトなし
.NET Framework クラス ライブラリ
List<T>.Reverse メソッド
この記事は翻訳者によって翻訳されたものです。 このページおよび元の英語コンテンツを同時に表示させるには、[ライトウェイト] に切り替えます。

List<T> 全体の要素の順序を反転させます。

名前空間:  System.Collections.Generic
アセンブリ:  mscorlib (mscorlib.dll 内)
構文

Visual Basic
Public Sub Reverse
C#
public void Reverse()
Visual C++
public:
void Reverse()
F#
member Reverse : unit -> unit 

解説

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

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


Reverse メソッドの 2 種類の使用方法をオーバーロードしたコード例を次に示します。 コード例では、文字列の List<T> を作成し、6 つの文字列を追加します。 Reverse() メソッドのオーバーロードを使用してリストの順序を反転し、次に Reverse(Int32, Int32) メソッドのオーバーロードを使用して、要素 1 から始まり 4 つの要素を含んだリストの中間部分の順序を反転します。

Visual Basic

Imports System
Imports System.Collections.Generic

Public Class Example

    Public Shared Sub Main()

        Dim dinosaurs As New List(Of String)

        dinosaurs.Add("Pachycephalosaurus")
        dinosaurs.Add("Parasauralophus")
        dinosaurs.Add("Mamenchisaurus")
        dinosaurs.Add("Amargasaurus")
        dinosaurs.Add("Coelophysis")
        dinosaurs.Add("Oviraptor")

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        dinosaurs.Reverse()

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        dinosaurs.Reverse(1, 4)

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

    End Sub
End Class

' This code example produces the following output:
'
'Pachycephalosaurus
'Parasauralophus
'Mamenchisaurus
'Amargasaurus
'Coelophysis
'Oviraptor
'
'Oviraptor
'Coelophysis
'Amargasaurus
'Mamenchisaurus
'Parasauralophus
'Pachycephalosaurus
'
'Oviraptor
'Parasauralophus
'Mamenchisaurus
'Amargasaurus
'Coelophysis
'Pachycephalosaurus


C#

using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        List<string> dinosaurs = new List<string>();

        dinosaurs.Add("Pachycephalosaurus");
        dinosaurs.Add("Parasauralophus");
        dinosaurs.Add("Mamenchisaurus");
        dinosaurs.Add("Amargasaurus");
        dinosaurs.Add("Coelophysis");
        dinosaurs.Add("Oviraptor");

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        dinosaurs.Reverse();

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }

        dinosaurs.Reverse(1, 4);

        Console.WriteLine();
        foreach(string dinosaur in dinosaurs)
        {
            Console.WriteLine(dinosaur);
        }
    }
}

/* This code example produces the following output:

Pachycephalosaurus
Parasauralophus
Mamenchisaurus
Amargasaurus
Coelophysis
Oviraptor

Oviraptor
Coelophysis
Amargasaurus
Mamenchisaurus
Parasauralophus
Pachycephalosaurus

Oviraptor
Parasauralophus
Mamenchisaurus
Amargasaurus
Coelophysis
Pachycephalosaurus
 */


Visual C++

using namespace System;
using namespace System::Collections::Generic;

void main()
{
    List<String^>^ dinosaurs = gcnew List<String^>();

    dinosaurs->Add("Pachycephalosaurus");
    dinosaurs->Add("Parasauralophus");
    dinosaurs->Add("Mamenchisaurus");
    dinosaurs->Add("Amargasaurus");
    dinosaurs->Add("Coelophysis");
    dinosaurs->Add("Oviraptor");

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs)
    {
        Console::WriteLine(dinosaur);
    }

    dinosaurs->Reverse();

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs)
    {
        Console::WriteLine(dinosaur);
    }

    dinosaurs->Reverse(1, 4);

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs)
    {
        Console::WriteLine(dinosaur);
    }
}

/* This code example produces the following output:

Pachycephalosaurus
Parasauralophus
Mamenchisaurus
Amargasaurus
Coelophysis
Oviraptor

Oviraptor
Coelophysis
Amargasaurus
Mamenchisaurus
Parasauralophus
Pachycephalosaurus

Oviraptor
Parasauralophus
Mamenchisaurus
Amargasaurus
Coelophysis
Pachycephalosaurus
 */


バージョン情報

.NET Framework

サポート対象: 4、3.5、3.0、2.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 (Server Core はサポート対象外), Windows Server 2008 R2 (SP1 以降で Server Core をサポート), Windows Server 2003 SP2

.NET Framework では、各プラットフォームのすべてのバージョンはサポートしていません。 サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
参照

参照