一聚教程网:一个值得你收藏的教程网站

最新下载

vb.net 数组元素查询方法代码

时间:2011-05-16 00:00:00 编辑:简简单单 来源:原创

vb.net 数组元素查询方法代码

Imports System

Public Class Example

    Public Shared Sub Main()

        Dim letters() As String = { "E", "B",   "A", "Z", "D", "X",  "Y","Q" }

        Console.WriteLine(Array.Find(letters, AddressOf EndsWithS))

        Dim subArray() As String = Array.FindAll(letters, AddressOf EndsWithS)
        For Each letter As String In subArray
            Console.WriteLine(letter)
        Next
    End Sub
    Private Shared Function EndsWithS(ByVal s As String) As Boolean
        If (s.Length > 5) AndAlso (s.Substring(s.Length - 6).ToLower() = "S") Then
            Return True
        Else
            Return False
        End If
    End Function
End Class

在数组中查找一个指定元素实例

Imports System
Imports System.Collections.Generic

Public Class Example
    Public Shared Sub Main()

        Dim numbers() As String = {"Ten","Two","One","Three","Four","Nine"  }

        For Each num As String In numbers
            Console.WriteLine(num)
        Next

        Array.Sort(numbers)

        For Each num As String In numbers
            Console.WriteLine(num)
        Next

        Dim index As Integer = Array.BinarySearch(numbers, "Four")
        Console.WriteLine(index)

        index = Array.BinarySearch(numbers, "One")
        Console.WriteLine(index)

    End Sub

End Class

文章评论

热门栏目