033ArrayList的二分法查找

       BinarySearch方法使用二分算法从集合中快速搜索指定的值,并返回找到元素的索引,否则返回-1。

BinarySearch(Int32, Int32, Object, IComparer)

使用指定的比较器在已排序 ArrayList 的某个元素范围中搜索元素,并返回该元素从零开始的索引。

BinarySearch(Object)

使用默认的比较器在整个已排序的 ArrayList 中搜索元素,并返回该元素从零开始的索引。

BinarySearch(Object, IComparer)

使用指定的比较器在整个已排序的 ArrayList 中搜索元素,并返回该元素从零开始的索引。

      如果ArrayList包含具有同一值的多个元素,则该方法只返回一个匹配项,并且可能返回任何一个匹配项,而不一定是第一个。如果ArrayList不包含指定值,则该方法会返回负整数。可以将按位反码运算应用于此负整数,以获取大于搜索值的第一个元素的索引。
 

Public Class Book
    Private _bookname As String
    Private _bookprice As Double
    Private _bookCategory As String
    Public Property bookname As String
        Get
            Return _bookname
        End Get
        Set(ByVal value As String)
            _bookname = value
        End Set
    End Property
    Public Property bookprice As Double
        Get
            Return _bookprice
        End Get
        Set(ByVal value As Double)
            _bookprice = value
        End Set
    End Property
    Public Property bookcategory As String
        Get
            Return _bookCategory
        End Get
        Set(ByVal value As String)
            _bookCategory = value
        End Set
    End Property
    Public Sub New(ByVal name As String, ByVal price As Double, ByVal category As String)
        bookname = name
        bookprice = price
        bookcategory = category
    End Sub
End Class
Public Class ComparerByPrice
    Implements IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        If TypeOf x Is Book And TypeOf y Is Book Then
            Dim obj1 As Book = CType(x, Book)
            Dim obj2 As Book = CType(y, Book)
            If obj1.bookprice < obj2.bookprice Then
                Return -1
            ElseIf obj1.bookprice = obj2.bookprice Then
                Return 0
            Else
                Return 1
            End If
        Else
            Throw New ArgumentException("对象不是Book类型")
        End If
    End Function
End Class

Public Class ComparerByName
    Implements IComparer

    Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer Implements System.Collections.IComparer.Compare
        '忽略字母大小,比较两个字符串
        Return New CaseInsensitiveComparer().Compare(CType(x, Book).bookname, CType(y, Book).bookname)
    End Function
End Class


Module Module1

    Sub Main()
        Dim books() As Book = {New Book("C#入门", 45.5, "C#"), _
                               New Book("Visual Basic.Net 入门", 55.8, "Visual Basic"), _
                               New Book("Delphi程序设计", 78, "Delphi"), _
                               New Book("Visual C++.Net 技术内幕", 66, "C++"), _
                               New Book("ASP.NET", 44, "ASP.NET")}
        Dim myAL As ArrayList = New ArrayList(books)
        Console.WriteLine("列表ArrayList的初始内容是:")
        PrintValues(myAL)

        Dim mybook As Book = New Book("C#入门", 45.5, "C#")
        Dim index As Integer = myAL.BinarySearch(mybook, New ComparerByName)
        If index < 0 Then
            Console.WriteLine("没找到")
        Else
            Console.WriteLine("找到图书《{0}》所在索引为:{1}", mybook.bookname, index)
        End If

        Console.Read()
    End Sub

    Private Sub PrintValues(ByVal list As ICollection)
        Dim i As Integer
        Dim obj As Book
        For Each obj In list
            Console.WriteLine("[{0}]  " & "书名:" & obj.bookname.ToString.PadRight(25) & vbTab & "价格:" & obj.bookprice.ToString.PadRight(10), i)
            i = i + 1
        Next
        Console.WriteLine()
    End Sub
End Module

 

你可能感兴趣的:(#,Visual,Basic,2010,基础)