您可以使用Dictionary http://msdn.microsoft.com/en-us/library/x525za90.aspx#Y456通过将两个时间字符串连接在一起来跟踪时间范围Key
(不需要转换为整数),然后对于该范围的每次出现,您可以增加Value
的一部分Dictionary
.
此外,使用此方法不需要嵌套循环,也不需要分段循环来填充开始和结束时间数组。以下算法应该提供您正在寻找的结果:
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")
I've verified该算法具有类似的测试代码:
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)
Update
阅读评论中的说明后,您仍然可以使用字典以及几个帮助器类来帮助跟踪这一点(TimeRange
, TimeRangeCounter
, and 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)