是否有更快的方法从List(of T)中找到一个项目?



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

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

看起来您正在创建三个并行列表。为什么不创建一个3种类型的小类并创建一个单独的列表呢?我向VecThetaPhi类添加了一个构造函数,以使构造更容易。然后一点点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

最新更新