How to clear a list without losing all positions?

Asked

Viewed 127 times

0

In my application in C#, I have a list List< float> where I store data arriving through serial port. With the execution of the application in progress this list begins to get huge. I am not able to clean it because it is a real-time monitoring where I draw graphs with the values of this list and in these graphs are used the 2000 last positions of this list. My question is: How can I clear this list without losing the values of the last 2000 positions? Or maybe, how to make this list temporary, where when I reach N positions is created another list and stored the 2000 last positions of the previous list?

  • 1

    You can use Lynne

  • 4

    Certainly the correct solution is another, but without knowing details and we have no way to help. Cleaning doesn’t make much sense, even from what you’re saying you may have competition problems. Maybe you want to use a queue and not a list.

  • If we provide more information we can give a more objective answer

  • 1

    Man, I tried to explain to the max, the code is too big to stick here, but the idea of Maniero seems to work, I hadn’t thought of using a queue

  • 3

    Like I said, this is an XY problem, you want the solution to the wrong problem.

  • 3

    You are seeking efficiency, and the solutions presented are not inefficient.

Show 1 more comment

3 answers

5

You can do this way, the code is commented, explaining its operation

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
    static List<float> lista = new List<float>(2000);

    public static void addItemLista(float valor){
        //Se a lista ainda não completou os 2000 registro simplesmente adiciona mais um registro
        if(lista.Count < 2000){
            lista.Add(valor);
        }else{//Se tem mais de 2000 registro, ele remove o primeiro e adiciona o novo item
            lista.Remove(lista.FirstOrDefault());
            lista.Add(valor);
        }
    }

    public static void Main()
    {   
        //Teste adicionando 2020 registro
        for (int i = 0; i < 2020; i++)
        {
            addItemLista(i);
        }

        //Resultado
        lista.ForEach(i => Console.Write("{0}\t", i));
    }
}

Execute

3


lista = lista.Skip(lista.Count - 2000).ToList();
  • 1

    really hadn’t thought of that solution

  • thanks friend, it worked very well]

2

Let’s imagine the following list

List<float> lista = new List<float>();

To delete the entire list

 lista.Clear();

So to delete keep the last 2000 what I would do is create a list based on that requirement

var novaLista = (from p in lista 
                        orderby p descending
                        select p).Take(2000);

or else

for (int i = 0; i <= lista.Count -2001; i++)
{
   lista.RemoveAt(i);
}

using Queue

Queue Q = new Queue();

while(Q.Count > 2000)
{
  Q.Dequeue()
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.