用好Array的话对性能提高 或有很大帮助
声明Array的两种方式
'Dim arr1 As Array = Array.CreateInstance(GetType(String), 长度)
给定明确的类型:假设为string的数组
Dim arr1(长度 - 1) As String
利用Array.Sort()给指定的数组或数据key,数据value排序
下文是对Hashtable的key进行排序:(包括按字符升序或降序,字符升序的升序或降序)
Dim htPara As Hashtable = New Hashtable
Dim strLog As StringBuilder
Dim str As New StringBuilder
Dim arr As New ArrayList
初始化Hashtable的内容
htPara.Add("SHIP_ORDER_DATE", "2011/02/16")
htPara.Add("SHIP_ORDER_DATE_TOMORROW", "2011/02/17")
htPara.Add("a", "2011/02/18")
htPara.Add("aa", "2011/02/18")
htPara.Add("z", "2011/02/19")
初始化Hashtable的内容(数值型)
'htPara.Add(1, "2011/02/16")
'htPara.Add(2, "2011/02/17")
'htPara.Add(300, "2011/02/17")
Dim enumerator As IDictionaryEnumerator
enumerator = htPara.GetEnumerator
Dim arr1(htPara.Count - 1) As String
Dim arr2(htPara.Count - 1) As String
把Hashtable的内容复制到Array中
htPara.Keys().CopyTo(arr1, 0)
htPara.Values.CopyTo(arr2, 0)
如果内容是数值就可以行Sum()
Debug.WriteLine(arr1.Max())
Debug.WriteLine(arr1.Min())
'Debug.WriteLine(arr1.Sum())
把Array中的内容复制到 List中
'Dim list As List(Of Integer) = arr1.ToList
利用排序类进行排序(文字長さでソート(降順))
只是按文字的升序的话,可以不用第2个参数。保持默认即可
Array.Sort(arr1, New SortBySizeDesc)
按文字的升序排序,对像是index的1到3
Array.Sort(arr1, 1,3)
按文字的升序排序,对像是数组key,数组value排序
Array.Sort(arr1, arr2)
如果是Arraylist对像的话,还可以这样用。
假设Arraylist对像 是myArrlst
利用排序类进行排序(文字長さでソート(降順))
myArrlst.Sort(New SortBySizeDesc)
以下是排序类:只是按文字的升序的话,可以不用这个参数。保持默认即可
-----------------------------------------------
''' <summary>
''' 文字長さでソート(昇順)
''' </summary>
''' <remarks>ソート用</remarks>
Public Class SortBySizeAsc
Implements IComparer
Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements IComparer.Compare
Return New CaseInsensitiveComparer().Compare(x.ToString.Length, y.ToString.Length)
End Function
End Class
''' <summary>
''' 文字長さでソート(降順)
''' </summary>
''' <remarks>ソート用</remarks>
Public Class SortBySizeDesc
Implements IComparer
Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements IComparer.Compare
Return New CaseInsensitiveComparer().Compare(y.ToString.Length, x.ToString.Length)
End Function
End Class
''' <summary>
''' 文字でソート(昇順)
''' </summary>
''' <remarks>ソート用</remarks>
Public Class SortByCharAsc
Implements IComparer
Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements IComparer.Compare
Return New CaseInsensitiveComparer().Compare(x, y)
End Function
End Class
''' <summary>
''' 文字でソート(降順)
''' </summary>
''' <remarks>ソート用</remarks>
Public Class SortByCharDesc
Implements IComparer
Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
Implements IComparer.Compare
Return New CaseInsensitiveComparer().Compare(y, x)
End Function
End Class