ArrayList.Reverse Metódus
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Megfordítja az elemek sorrendjét a ArrayList benne vagy annak egy részében.
Túlterhelések
| Name | Description |
|---|---|
| Reverse() |
Az elemek sorrendjének megfordítása az egészben ArrayList. |
| Reverse(Int32, Int32) |
Megfordítja a megadott tartomány elemeinek sorrendjét. |
Reverse()
- Forrás:
- ArrayList.cs
- Forrás:
- ArrayList.cs
- Forrás:
- ArrayList.cs
- Forrás:
- ArrayList.cs
- Forrás:
- ArrayList.cs
Az elemek sorrendjének megfordítása az egészben ArrayList.
public:
virtual void Reverse();
public virtual void Reverse();
abstract member Reverse : unit -> unit
override this.Reverse : unit -> unit
Public Overridable Sub Reverse ()
Kivételek
Az ArrayList írásvédett.
Példák
Az alábbi példakód bemutatja, hogyan fordíthatja vissza az értékek rendezési sorrendjét egy ArrayList.
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
Megjegyzések
Ez a módszer az elemek sorrendjének megfordítására használjaArray.Reverse, így az [i] helyen lévő elem ArrayList , ahol i a tartomány bármely indexe, [j] értékre ArrayList kerül, ahol a j egyenlő index + index + count - i - 1.
Ez a metódus egy O(n) művelet, ahol n van Count.
A következőre érvényes:
Reverse(Int32, Int32)
- Forrás:
- ArrayList.cs
- Forrás:
- ArrayList.cs
- Forrás:
- ArrayList.cs
- Forrás:
- ArrayList.cs
- Forrás:
- ArrayList.cs
Megfordítja a megadott tartomány elemeinek sorrendjét.
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)
Paraméterek
- index
- Int32
A megfordítandó tartomány nullaalapú kezdőindexe.
- count
- Int32
A megfordítandó tartomány elemeinek száma.
Kivételek
index és count ne jelölje az elemek érvényes tartományát a ArrayList.
Az ArrayList írásvédett.
Példák
Az alábbi példakód bemutatja, hogyan fordíthatja vissza az értékek rendezési sorrendjét ArrayListegy elemtartományban.
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
Megjegyzések
Ez a módszer az elemek sorrendjének megfordítására használjaArray.Reverse, így az [i] helyen lévő elem ArrayList , ahol i a tartomány bármely indexe, [j] értékre ArrayList kerül, ahol a j egyenlő index + index + count - i - 1.
Ez a metódus egy O(n) művelet, ahol n van count.