Hi,

I have a List containing double values and a double mean.

I need to check and store all 5 consecutive values (in indivuals lists) that are higher that mean value.

At this moment my solution always clear my aux List :( any ideas?

``````double media = 1.8815;
List<double> lista = new List<double>();

List<double> aux = new List<double>();

for (int i=0; i < lista.Count; i++) {

if(lista[i] > media && aux.Count <5){
}
else{aux.Clear();}

foreach (double valor in aux) { Console.WriteLine(valor); }
``````

It clears because cooner or later your code goes to else block, and clears the list.

## But I actually dont get it which value would you like to put into aux list from lista. Can you clarify it a bit better?

Do you mean that tin the for loop you check the current value, and if this and next 5 values are higher then the media value, you put all these 5 values into aux list?
And you continue loopint to the end of the items in lista?

If so, you can do it like:

``````List<double> aux = new List<double>();
int counter = 0;
for (int i = 0; i < lista.Count; i++)
{
if(counter == 5)
break;
if(lista[i] > media && counter <= 5)
{
counter++;
}
else
{
counter = 0;
aux.Clear();
}
}

//show values...
``````
commented: 1 +1

Yes, i want to check all list and every time that a value and the 4 next values are higher than media value I copy that small list of 5 and copy it... and continue looping to the end of the list.

Thanks a lot, its perfect! :D

Sorry Bonca, your code proposal doesnt continue looping the list to the end :( any ideas to go around this?

It stops when find 5 consecutive values higher than media.

You means to add all numbers (of 5 in a row) if higher then the media into new list?

yes, and then retrieve the a big list containing all small lists of 5 elements :(

In this case, we have to re-do your code completely. Usin some Linq and an additional method. I did think of something like this:

``````        private void YourMethod()
{
double media = 1.8815;
List<double> lista = new List<double>();
//and other values...

List<double> aux = new List<double>();
for (int i = 0; i < lista.Count; i++)
{
if(CheckNext5(media, lista.Skip(i).Take(5).ToList()))
{
for (int j = 0; j < 5; j++)
i = i + 4;
}
}
}

private bool CheckNext5(double media, List<double> list)
{
bool bFlag = true;
foreach (double item in list)
{
if (item < media)
{
bFlag = false;
break;
}
}
return bFlag;
}
``````

There is a method, that checked next 5 values if they are higher then the media variable, is so, they are added to the new list, else, loops goes on to get next values.
ps: vote and add a comment :)

Hope you like it :)

Now its really perfect! Thats it :) Thank you very much for our time.