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

热门教程

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

时间:2022-06-25 05:34:07 编辑:袖梨 来源:一聚教程网

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

热门栏目