user3363022 user3363022 - 4 months ago 9
C# Question

When i iterate through count, my array changes size, why?

When I run a simple code designed to make a training and testing data cross-validate, I iterate through each count of my list but each time it iterates the list decreases by 1.

for (var i = 0; i < adjustedData.Count; i++)
{
var tempData = adjustedData;
var testData = tempData[i].ToArray();
var trainDataList = tempData;
trainDataList.RemoveAt(i);
var trainData = trainDataList.ToArray();

Answer

Shouldn't that be cause your code is removing the item from array/list. The below code line

       trainDataList.RemoveAt(i);