代码之家  ›  专栏  ›  技术社区  ›  Anders

是否可以对每个循环进行反向操作?

  •  33
  • Anders  · 技术社区  · 16 年前

    我认为传统方法不可能做到这一点,但类似于以下详细代码:

    For Each s As String In myStringList Step -1
        //' Do stuff here
    Next
    

    我可能必须先反转myString对象,然后再执行常规For..每个循环,对吗?

    13 回复  |  直到 16 年前
        1
  •  48
  •   Ian    6 年前

    我认为文件中引用了 Mike's answer below 极其 For Each 由其调用的集合(即其实现)定义 IEnumerable / IEnumerable<T> 这和说在订单很重要的时候不应该使用它是一样的。许多集合(如数组、, List<T> 等)始终按照“自然”顺序进行。

    部分文件确实提到了这一点:

    . 当您执行 对于每个…下一个循环,遍历 集合由 GetEnumerator方法。秩序 遍历不是由视觉特性决定的 基本的,但更确切地说是下一步 枚举器对象的方法。这 意味着你可能无法 预测元素的哪一个 集合是第一个要返回的 在元素中,或者下一个 在给定元素之后返回。

    这与说它不可依赖完全不同——它可以依赖 . 它不像是随机选取元素。行为方面 数不清 / IEnumerable<T> 在那一页上有明确的定义。

    逆转 使用 Enumerable.Reverse -但是,如果需要对按位置索引的集合(如数组或数组)进行反向迭代 )那么,使用 For 循环从末尾开始,向后运行。

        2
  •  43
  •   SqlRyan    16 年前

    您必须执行以下操作:

    For i as integer = myStringList.Count-1 to 0 step -1
        dim s as string = myStringList.Item(i)
        ' Do your stuff with s
    Next i
    

    但据我所知,你不能做一个“为…每一个”向后,虽然在一些情况下,这将是很好的。

        3
  •  13
  •   Mike    16 年前

    不幸的是 MSDN docs on For Each

    祝你好运

        4
  •  12
  •   Amy B    10 年前

    System.Linq.Enumerable.Reverse 方法获取一个 IEnuemrable(Of TSource) 与可枚举源的顺序相反。

        5
  •  4
  •   Dave    10 年前

    For Each s As String In myStringList.Reverse
        //' Do stuff here
    Next
    

    它不起作用,正确的方法是:

    myStringList.Reverse() ' it is a method not a function
    For Each s As String In myStringList
    //' Do stuff here
    Next
    

    看看: MSDN: Reserve

        6
  •  3
  •   Tonko Boekhoud    10 年前
    For Each s As String In myStringList.Reverse
        //' Do stuff here
    Next
    
        7
  •  1
  •   reinierpost    16 年前

    它无法完成的原因是,作为一个基本的设计特性,For-Each可以在没有最后一个元素的情况下迭代一个可枚举项,或者使用一个未知的最后一个元素。

        8
  •  1
  •   MontanaMan    4 年前

    这项工作:

    Dim myArray() As String = {"1", "2", "3", "4", "5"}
    
    For Each n As String In myArray.Reverse
        Debug.Print(n)
    Next
    

    产出:54321

    这也适用于:

    Dim myArray() As String = {"1", "2", "3", "4", "5"}
    
    For i As Integer = myArray.Length - 1 To 0 Step -1
        Debug.Print(myArray(i))
    Next
    

    产出:54321

        9
  •  0
  •   xxxmandoxxx    11 年前

    您需要做的是使用for-each创建一个数组,然后使用array.reverse并在数组上运行for-each。多恩

    干杯

        10
  •  0
  •   Michael J. Berry    10 年前

    根据循环中发生的情况,可以使用.InsertAt(对象,0)而不是.Add,并生成与反向枚举相同的结果。

        11
  •  0
  •   DotNet Programmer    9 年前

    可以将扩展函数添加到要反转的类中

    <Serializable()> Public Class SomeCollection
        Inherits CollectionBase
        Public Sub New()
        End Sub
    
        Public Sub Add(ByVal Value As Something)
            Me.List.Add(Value)
        End Sub
    
        Public Sub Remove(ByVal Value As Something)
            Me.List.Remove(Value)
        End Sub
    
        Public Function Contains(ByVal Value As Something) As Boolean
            Return Me.List.Contains(Value)
        End Function
    
        Public Function Item(ByVal Index As Integer) As Something
            Return DirectCast(Me.List.Item(Index), Something)
        End Function
    
        Public Function Reverse() As SomeCollection
            Dim revList As SomeCollection = New SomeCollection()
            For index As Integer = (Me.List.Count - 1) To 0 Step -1
                 revList.List.Add(Me.List.Item(index))
            Next
            Return revList
        End Function
    End Class
    

    For Each s As Something In SomeCollection.Reverse
    
    Next
    
        12
  •  0
  •   Robert Mars_win    9 年前

    首先,您应该为每个语句在中创建一个(字符串)列表,然后为..创建另一个正常列表。。步骤1.下一个语句。请参见一个示例:

    Dim ff As New List(Of Integer)
    For Each rRow As DataRow In DeletedRows
    Dim item As Integer
    item = rRow.Table.Rows.IndexOf(rRow)
    ff.Add(item)
    Next
    For i As Integer = ff.Count - 1 To 0 Step -1
    dim item as integer=ff(i)
    next i
    

    我希望这会有帮助

        13
  •  0
  •   Adam    8 年前

    接受的答案解释了原因,但要添加另一个示例,对于带有键的集合(SortedList、SortedDictionary、Dictionary等),您可以执行以下操作

    For Each Id as Tkey in MyCollection.Keys.Reverse
       // The item in question is now available as MyCollection(Id)
    Next
    
        14
  •  0
  •   Georg    8 年前

    '创建任何内容的列表并逐步执行循环收集

    dim revList as New List (of ToolStripItem)  
    For each objItem as ToolStripItem in Menu.DropDownItems
        revList.Add(objItem)
    next
    revList.reverse ' reverse the order of that list
    

    for each objItem as ToolStripItem in revList   
        ' do whatever (like removing your menu from an ArrayMenu)
    next