Answer the question
In order to leave comments, you need to log in
How to get 5 elements from an array?
There is an array in it 23 elements, maybe more, maybe less
How to receive 5 elements and perform an action
That is, received 5 elements, displayed a message received
And so on until the end of the array.
Answer the question
In order to leave comments, you need to log in
C#
1.2 list.Skip(n).Take(5)
.
var slice = new ArraySegment<string>(list, n, n + 5)
var queue = Queue<string>() { ... };
var el1 = queue.Dequeue()
var el2 = queue.Dequeue()
var el3 = queue.Dequeue()
var el4 = queue.Dequeue()
var el5 = queue.Dequeue()
Use the array_chunk function to split an existing array into several other N-element arrays.
php.net/manual/en/function.array-chunk.php
$chunks = array_chunk($array, 5);
foreach($chunks as $chunk) {
print_r($chunk);
}
View the remainder after dividing the increment by 5. If 0, then display your message.
The short answer, as always, is easily found on the Internet.
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var victimArray = new string[] {
"1", "3", "2", "1", "23",
"44", "32", "2", "10", "0",
"17", "-1", "34", "79", };
var expectedIndexesWithValues = new List<string>() {
{"23"}, // "пятый" элемент
{"0"} // следующий "пятый" элемент
};
var actualIndexesWithValues = new List<string>();
victimArray.RunActionOnEach(eachElementNumber: 5,
actionOnElement: (elementValue) => {
Console.WriteLine($"Какое-то жизненно необходимое действие на элементе \"{elementValue}\"");
actualIndexesWithValues.Add(elementValue);
}
).ToList();
// проверяем, что результат совпадает с нашими ожиданиями
CollectionAssert.AreEquivalent(expectedIndexesWithValues, actualIndexesWithValues);
}
}
internal static class IEnumerableExtension
{
/// <summary>
/// Дергает переданный Action на каждом eachElementNumber элементе
/// </summary>
public static IEnumerable<string> RunActionOnEach(this IEnumerable<string> sourceCollection, int eachElementNumber, Action<string> actionOnElement)
{
int currentNumber = 1;
foreach (var element in sourceCollection)
{
if (currentNumber % eachElementNumber == 0)
actionOnElement(element);
currentNumber++;
yield return element;
}
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question