VB.NET在arraylist中查找值 [英] VB.NET Find value in arraylist

查看:715
本文介绍了VB.NET在arraylist中查找值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有这样的结构:



Hi all,

I have this structure:

Public strcMyInfo As DISPLAYDIRECTORY
Public arrDirectory As ArrayList

Public Structure DISPLAYDIRECTORY
    Dim strdirno As String
    Dim strdirname As String
    Dim strdirdetails As String
    Dim strcategory As String
    Dim strwebsite As String
    Dim strphoneno As String
End Structure



我将对数据库进行查询并将结构strcMyInfo添加到arrDirectory 。 arrDirectory将保存包含strcMyInfo数据的数据,例如10个索引。例如,arrDirectory.Item(6).strdirname的值是G2000。如何通过arraylist循环找到G2000的值以及strdirno,strdirdetails,strcategory,strwebsite和strphoneno?



我搜索了互联网,但他们只看如下所示添加1值:


I will do a query to database and add structure strcMyInfo to arrDirectory. The arrDirectory will hold data which contains the strcMyInfo data let's say 10 index. For example, the value of arrDirectory.Item(6).strdirname is G2000. How can I loop through the arraylist to find the value of G2000 together with strdirno, strdirdetails,strcategory,strwebsite and strphoneno?

I have search for internet but they only look for a 1 value when adding as example below:

myAL.Add("the")
myAL.Add("quick")
myAL.Add("brown")
myAL.Add("fox")
myAL.Add("jumps")
myAL.Add("over")
myAL.Add("the")
myAL.Add("lazy")
myAL.Add("dog")





但是我的代码是这样的:



But my code will be like this:

If (rdr.HasRows()) Then
     arrDirectory = Nothing
     arrDirectory = New ArrayList
     While rdr.Read
          With strcSearchDir
                  If Not IsDBNull("dirno") Then
                            .strdirno = (rdr("dirno"))
                  Else
                            .strdirno = "N/A"
                  End If
                  If Not IsDBNull("dirname") Then
                            .strdirname = (rdr("dirname"))
                  Else
                            .strdirname = "N/A"
                  End If
                  If Not IsDBNull("dirdetails") Then
                            .strdirdetails = (rdr("dirdetails"))
                  Else
                            .strdirdetails = "N/A"
                  End If
                  If Not IsDBNull("category") Then
                            .strcategory = (rdr("category"))
                  Else
                            .strcategory = "N/A"
                  End If
                  If Not IsDBNull("website") Then
                            .strwebsite = (rdr("website"))
                  Else
                            .strwebsite = "N/A"
                  End If
                  If Not IsDBNull("phoneno") Then
                            .strphoneno = (rdr("phoneno"))
                  Else
                            .strphoneno = "N/A"
                  End If
          End With
          arrDirectory.Add(strcSearchDir)
     End While
     Return True
Else
     Return False
End If





下面是找到字符串的代码,但它停在那里因为我不知道如何继续:





Below are code to find the string but it stop there because i don't know how to continue:

Private Sub GetMyDetails(ByVal strLabel As Label)
        Dim obj As Object
        Try
            If strLabel.Content <> String.Empty Then
                For Each obj In arrDirectory
                    If arrDirectory.IndexOf(obj) = strLabel.Content Then

                    End If
                Next
            End If
        Catch ex As Exception

        End Try
End Sub





如果有人知道如何在arrayli中使用indexof st,请指导我。谢谢



If someone know how to use the indexof in arraylist, please guide me. Thanks

推荐答案

永远不要使用 ArrayList 。这个类早在.NET v.2.0中就已经过时了,引入了泛型。如果没有正式标记为过时,只是因为它足以将其留在某些遗留代码中。它的潜在问题是需要类型转换,可能的错误的额外来源(你为什么引入泛型?)。



相反,使用泛型类 System.Collections.Generic.List<>

http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx [<一个href =http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspxtarget =_ blanktitle =New Window> ^ ]。



对于搜索,您可以使用名为查找 FindIndex ,使用谓词参数,因此您可以按任何搜索条件进行搜索:

http://msdn.microsoft.com/en-us/library/0k601hd9%28v=vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library /x0b5b5bc%28v=vs.110%29.aspx [ ^ ]。



上面引用的MSDN文档非常清楚,所以我希望你能解决你的搜索问题很容易。



当然,这种搜索的时间复杂度是O(N)(线性)。你可以有复杂的O(1)(渐近,不依赖于数据的大小),基于支持的集合,通过某些键索引(你需要根据你的搜索条件创建) ):

http:// msdn。 microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx [ ^ ],

http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx [ ^ ],

http:// msdn.microsoft.com/en-us/library/ms132319%28v=vs.110%29.aspx [ ^ ]。



请参阅: http://en.wikipedia.org/wiki/Big_O_notation [ ^ ]。



我没有解释与密钥相关的部分-indexed集合,因为这不是问题的一部分,但你最好学习这些类型,以及所有的集合,因为了解它们对你的工作非常重要。



-SA
Never ever use ArrayList. This class was rendered obsolete as early as of .NET v.2.0, with introduction of generics. If is not formally marked as obsolete only because it is good enough to leave it in some legacy code. Its potential problem is a need for type casts, an extra source of possible bugs (why do you thing generics were introduced for?).

Instead, use the generic class System.Collections.Generic.List<>:
http://msdn.microsoft.com/en-us/library/6sh2ey19%28v=vs.110%29.aspx[^].

For search, you can use the methods named Find or FindIndex, using a predicate parameter, so you can search by any search criteria:
http://msdn.microsoft.com/en-us/library/0k601hd9%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/x0b5b5bc%28v=vs.110%29.aspx[^].

The MSDN documentation referenced above is crystal clear, so I hope you can solve your problem with search easily.

Naturally, the time complexity of such search is O(N) (linear). You can have complexity of O(1) (asymptotically, not depending on the size of data), with collections based on backets with indexing by some key (which you would need to create based on your search criteria):
http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/xfhwa508%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/ms132319%28v=vs.110%29.aspx[^].

Please see: http://en.wikipedia.org/wiki/Big_O_notation[^].

I'm not explaining the part related to key-indexed collection because this is not a part of the question, but you better learn these types, as well as all the collections, as knowing them is very important for your work.

—SA


这篇关于VB.NET在arraylist中查找值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆