Задача.
Есть типизированная коллекция. Необходимо производить последовательную вычитку коллекции по частям. Например, коллекция из 10 элементов. Последовательно вычитать первые 4 элемента, вторые 4 элемента и тд.
Решение.
Sub Main()
Dim l As New List(Of Integer)
l.Add(1) : l.Add(2) : l.Add(3) : l.Add(4) : l.Add(5) : l.Add(6) : l.Add(7) : l.Add(8) : l.Add(9) : l.Add(10)
Dim total As Integer = l.Count
Dim sizeOfPage As Integer = 4
Dim qantityOfPages As Integer = CInt(Math.Truncate(total / sizeOfPage))
For i As Integer = 0 To qantityOfPages
Dim startPos As Integer = i * sizeOfPage
Dim innerList As List(Of Integer) = l.Skip(startPos).Take(sizeOfPage).ToList
For Each ed In innerList
Console.WriteLine(ed.ToString)
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
Ниже аналогичное решение, но полностью на foreach ... next и if ... end if
Sub Main()
Dim l As New List(Of Integer)
l.Add(1) : l.Add(2) : l.Add(3) : l.Add(4) : l.Add(5) : l.Add(6) : l.Add(7) : l.Add(8) : l.Add(9) : l.Add(10)
Dim total As Integer = l.Count
Dim sizeOfPage As Integer = 4
Dim qantityOfPages As Integer = CInt(Math.Truncate(total / sizeOfPage))
For i As Integer = 0 To qantityOfPages
Dim startPos As Integer = i * sizeOfPage
Dim endPos As Integer = startPos + sizeOfPage
For ed As Integer = startPos To endPos - 1
If ed < l.Count Then
Console.WriteLine(l.Item(ed).ToString)
Else
Exit For
End If
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
в обоих случаях получаем следущее:
Есть типизированная коллекция. Необходимо производить последовательную вычитку коллекции по частям. Например, коллекция из 10 элементов. Последовательно вычитать первые 4 элемента, вторые 4 элемента и тд.
Решение.
Sub Main()
Dim l As New List(Of Integer)
l.Add(1) : l.Add(2) : l.Add(3) : l.Add(4) : l.Add(5) : l.Add(6) : l.Add(7) : l.Add(8) : l.Add(9) : l.Add(10)
Dim total As Integer = l.Count
Dim sizeOfPage As Integer = 4
Dim qantityOfPages As Integer = CInt(Math.Truncate(total / sizeOfPage))
For i As Integer = 0 To qantityOfPages
Dim startPos As Integer = i * sizeOfPage
Dim innerList As List(Of Integer) = l.Skip(startPos).Take(sizeOfPage).ToList
For Each ed In innerList
Console.WriteLine(ed.ToString)
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
Ниже аналогичное решение, но полностью на foreach ... next и if ... end if
Sub Main()
Dim l As New List(Of Integer)
l.Add(1) : l.Add(2) : l.Add(3) : l.Add(4) : l.Add(5) : l.Add(6) : l.Add(7) : l.Add(8) : l.Add(9) : l.Add(10)
Dim total As Integer = l.Count
Dim sizeOfPage As Integer = 4
Dim qantityOfPages As Integer = CInt(Math.Truncate(total / sizeOfPage))
For i As Integer = 0 To qantityOfPages
Dim startPos As Integer = i * sizeOfPage
Dim endPos As Integer = startPos + sizeOfPage
For ed As Integer = startPos To endPos - 1
If ed < l.Count Then
Console.WriteLine(l.Item(ed).ToString)
Else
Exit For
End If
Next
Console.WriteLine()
Next
Console.ReadLine()
End Sub
в обоих случаях получаем следущее:
Комментариев нет:
Отправить комментарий