简体   繁体   English

VB:向后循环通过列表

[英]VB: Loop Backwards Through A List

I have set up a For Each statement that loops through a list. 我已经建立了一个遍历列表的For Each语句。 The list is made up of 'pixels' which is a custom picturebox class. 该列表由“像素”组成,这是一个自定义的图片框类。 I need to setup a loop that goes through the list in reverse order. 我需要设置一个以相反顺序遍历列表的循环。 This is what my normal loop looks like: 这是我的普通循环的样子:

        For Each Tile As Pixel In PixelList
            If Tile.PixelNumber = 1 Then
                Tile.NewColour()
            End If
        Next

PixelNumber is just a integer variable that keeps count of which pixel is which. PixelNumber只是一个整数变量,用于计数哪个像素是哪个像素。

Pixel is the custom Picturebox class 像素是自定义Picturebox类

PixelList is the name of the list PixelList是列表的名称

I need to be able to setup a similar loop but one that goes through the list in reverse order of when I Tile was added. 我需要能够设置一个类似的循环,但是循环的顺序与添加Tile时的顺序相反。 So a first in first out situation. 因此,先进先出的情况。

вʀaᴎᴅᴏƞ вєнᴎєƞ's way is the most efficient, but a simple alternative would be to use the List(Of T).Reverse() function , though that changes the order of the list itself. 方法是最有效的,但是一个简单的替代方法是使用List(Of T).Reverse()函数 ,尽管这会更改列表本身的顺序。

    PixelList.Reverse()
    For Each Tile As Pixel In PixelList
        If Tile.PixelNumber = 1 Then
            Tile.NewColour()
        End If
    Next

Access the list by an index that starts at the count of elements in the collection and decrements by one. 通过一个索引访问列表,该索引从集合中的元素数开始,然后递减1。

For i as Integer = PixelList.Count - 1 To 0 Step -1
    If DirectCast(PixelList(i), Pixel).PixelNumber = 1 then DirectCast(PixelList(i), Pixel).NewColour()
Next

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM