有没有更快的方法从 List(of T) 中查找项目?

Is there a faster way to find an item from a List(of T)?

我创建矢量并将它们存储在(Vector3D 的)列表中。为了稍后再次找到正确的向量,我还存储了 Phis 和 Thetas(都是 List (of Double))。我使用 getVector 函数根据用户设置的 theta 和 phi 找到正确的向量。 有没有更快的方法?

Public Sub New(ByVal radius As Double)
        Me.Radius = radius
        Camera_Vector = New Vector3D(0, 0, Camera)
        For _phi As Double = 0.0 To 359.5 Step 0.5
            For _theta As Double = 0.0 To 90.0 Step 0.5
                List_with_all_the_vectors.Add(New Vector3D(
                                                radius * Math.Cos(_phi * Math.PI / 180.0) * Math.Sin(_theta * Math.PI / 180.0),
                                                radius * Math.Sin(_phi * Math.PI / 180.0) * Math.Sin(_theta * Math.PI / 180.0),
                                                radius * Math.Cos(_theta * Math.PI / 180.0)))
                Liste_thetas.Add(_theta)
                Liste_phis.Add(_phi)
            Next
        Next
    End Sub

    Public Function getVector() As Vector3D
        Dim Value As Vector3D
        For i As Integer = 0 To List_with_all_the_vectors.Count - 1 Step 1
            If Liste_phis(i) = CDbl(Phi) AndAlso Liste_thetas(i) = CDbl(Theta) Then
                Value = List_with_all_the_vectors(i)
                Return Value
            End If
        Next
        Return Value
    End Function

您似乎正在创建三个并行列表。为什么不创建一点 class 的 3 种类型并创建一个列表。我在 VecThetaPhi class 中添加了一个构造函数,以使构造更容易。然后一点点 Linq 魔法(不一定比 for 循环快,但更酷)找到你正在寻找的向量。

Private Camera As Double
Private Camera_Vector As Vector3D
Private Radius As Double

Public Class VecThetaPhi
    Public Vect As Vector3D
    Public Theta As Double
    Public Phi As Double
    Public Sub New(vec As Vector3D, the As Double, ph As Double)
        Vect = vec
        Theta = the
        Phi = ph
    End Sub
End Class

Private LstVecThetaPhi As New List(Of VecThetaPhi)
Public Sub New(ByVal radius As Double)
    Me.Radius = radius
    Camera_Vector = New Vector3D(0, 0, Camera)
    For _phi As Double = 0.0 To 359.5 Step 0.5
        For _theta As Double = 0.0 To 90.0 Step 0.5
            Dim vec As New Vector3D(
                                    radius * Math.Cos(_phi * Math.PI / 180.0) * Math.Sin(_theta * Math.PI / 180.0),
                                    radius * Math.Sin(_phi * Math.PI / 180.0) * Math.Sin(_theta * Math.PI / 180.0),
                                    radius * Math.Cos(_theta * Math.PI / 180.0))
            LstVecThetaPhi.Add(New VecThetaPhi(vec, _theta, _phi))
        Next
    Next
End Sub

Public Function getVector(ThetaValue As Double, PhiValue As Double) As Vector3D
    Dim Value As Vector3D = (From item In LstVecThetaPhi
                             Where item.Theta = ThetaValue And item.Phi = PhiValue
                             Select item.Vect).FirstOrDefault
    Return Value
End Function