您可以使用Dictionary 来跟踪时间范围,方法是将Key 的两个时间字符串连接在一起(无需转换为整数),然后对于该范围的每次出现,您可以递增ValueDictionary 的一部分。
此外,使用此方法无需使用嵌套循环,也无需使用暂存循环来填充开始和结束时间数组。以下算法应提供您正在寻找的结果:
Dim columnLength As Integer = GV.Rows.Count
Dim timeRangeCount As New Dictionary(Of String, Integer)
For i = 0 to columnLength - 1
Dim timeStart As String = GV.Rows(i).Cells(0).Text
Dim timeEnd As String = GV.Rows(i).Cells(1).Text
Dim key As String = String.Format("{0}-{1}", timeStart, timeEnd)
If timeRangeCount.ContainsKey(key)
timeRangeCount(key) += 1
Else
timeRangeCount(key) = 1
End If
Next
因此,您可以通过以下方式从字典中提取时间范围 09.40 - 10.00 的计数:
Dim totalOccuredOnThisTime As Integer = timeRangeCount("09.40-10.00")
我已经用类似的测试代码验证了这个算法:
Dim columnLength As Integer = 6
Dim timeStart() As String = {"08.00", "08.00", "10.00", "08.00", "08.00", "10.00"}
Dim timeEnd() As String = {"08.50", "08.50", "11.00", "09.00", "08.50", "11.00"}
Dim timeRangeCount As New Dictionary(Of String, Integer)
For i = 0 to columnLength - 1
Dim key As String = String.Format("{0}-{1}", timeStart(i), timeEnd(i))
If timeRangeCount.ContainsKey(key)
timeRangeCount(key) += 1
Else
timeRangeCount(key) = 1
End If
Next
Console.WriteLine(timeRangeCount)
更新
在阅读了您在评论中的说明后,您仍然可以使用 Dictionary 以及几个辅助类(TimeRange、TimeRangeCounter 和 TimeRangeEqualityComparer)来帮助跟踪这一点。不过,这确实使事情变得更复杂了。
第一个帮助类用于保存精确匹配和子范围匹配的计数:
Public Class TimeRangeCounter
Property ExactRangeMatch as Integer
Property SubRangeMatch as Integer
End Class
第二个帮助类用于帮助字典了解一个键(TimeRange 类型)与另一个键有何不同:
Public Class TimeRangeEqualityComparer
Implements IEqualityComparer(Of TimeRange)
Public Overloads Function Equals(left As TimeRange, right As TimeRange) _
As Boolean Implements IEqualityComparer(Of TimeRange).Equals
Return left.ToString = right.ToString
End Function
Public Overloads Function GetHashCode(range As TimeRange) _
As Integer Implements IEqualityComparer(Of TimeRange).GetHashCode
return range.ToString().GetHashCode()
End Function
End Class
第三个助手类存储范围的开始和结束时间:
Public Class TimeRange
Private readonly _start
Private readonly _end
Public Readonly Property Start
Get
return _start
End Get
End Property
Public Readonly Property [End]
Get
return _end
End Get
End Property
Public Sub New(start As String, [end] As string)
Me._start = start
Me._end = [end]
End Sub
Public Overrides Function ToString() as String
Return String.Format("{0}-{1}", Start, [End])
End Function
End Class
所以使用上面我们应该可以写出这个算法:
Dim columnLength As Integer = 5
Dim timeStart() As String = {"08.00", "08.00", "10.00", "08.00", "08.00"}
Dim timeEnd() As String = {"08.50", "11.50", "11.00", "09.00", "08.50"}
Dim comparer As New TimeRangeEqualityComparer()
Dim timeRangeCounts As New Dictionary(Of TimeRange, TimeRangeCounter)(comparer)
'Count exact range matches while building dictionary
For i = 0 to columnLength - 1
Dim key As TimeRange = New TimeRange(timeStart(i), timeEnd(i))
If timeRangeCounts.ContainsKey(key)
timeRangeCounts(key).ExactRangeMatch += 1
Else
Dim counter = New TimeRangeCounter()
counter.ExactRangeMatch = 1
timeRangeCounts(key) = counter
End If
Next
'Count sub ranges
For Each kvp in timeRangeCounts
For Each key in timeRangeCounts.Keys
If kvp.key.Start >= key.Start AndAlso _
kvp.Key.End <= key.End AndAlso _
kvp.key.ToString <> key.ToString then
kvp.Value.SubRangeMatch += 1
End If
Next
Next
Console.WriteLine(timeRangeCounts)