我将客户端数据保存到数组列表中。如何使用快速方法在数组列表(clientList)中查找部分数据(currentIP)?
代码如下:
' Array list to keep Clients Object
Protected Friend clientList As ArrayList = ArrayList.Synchronized(New ArrayList())
Public Class Clients
public clientIPAdrress As IPAddress
public clientTCP As TcpClient
public clientStream As SslStream
End Class
Public Sub Test()
' Create objClients Object from Clients Class
Dim objClients as new Clients
' Add objClients to Array List
clientList.Add(objClients)
Dim currentIP as IPAdress = IPAddress.Parse("192.168.1.2")
Dim isIPFound as Boolean = False
' Search currentIP inside clientList with looping method
For Each ip As Clients In clientList
If ip.ClientIPAdrress = currentIP Then
isIPFound = True
Exit For
End If
Next
End Sub
感谢您的建议。
答案 0 :(得分:1)
您应该使用键入的List(Of Clients)
,然后cou可以使用LINQ:
Dim clientList As New List(Of Clients)
clientList.Add(new Clients())
Dim isIPFound=clientList.Any(Function(ip) ip.ClientIPAdrress = currentIP)
但基本上它与你的循环只是一行相同。
如果您需要找到最快的方法,可以使用自定义比较器对列表和BinarySearch IP进行排序。或者,如果列表中的IP是唯一的,则可以改为使用Dictionary(Of IPAdress,Clients)
。