0

I wrote a static method which reads scientific numbers(X,Y) from a text file and put them in a List of list<double>. But I don't know why the next value from file override all other values.

IF != 100 - 100 is first value of text file and its only property for my program.

 static List<List<double>> DownloadData(string path1)
    {
        List<List<double>> lista = new List<List<double>>();
        List<double> doubelowa = new List<double>();
        doubelowa.Clear();
        string line = null;
        try
        {

            using (TextReader sr = File.OpenText(path1))
            {
               
                while ((line = sr.ReadLine()) != null)
                {
                    doubelowa.Clear();
                    if (line != "100")
                    {
                        var d = line.Split().Select(f => double.Parse(f, System.Globalization.NumberStyles.Float, CultureInfo.InvariantCulture));
                        doubelowa.AddRange(d);
                        lista.Add(doubelowa);
                    }
                }
            }
        }
        finally
        {
            
        }
        return lista;
    }

Before I wrote this method and it worked great. But now when I write more and more code I don't know what changed. I try fix it but...

Its screen with locals: https://onedrive.live.com/redir?resid=DF3242C9A565ECD1!4549&authkey=!AEDu90t1iNQj4MY&v=3&ithint=photo%2cpng

For some reason the double.clear() clear the value of list Lista. Why?

2
  • This seems very complicated. Can you tell us what you want to do? In plain english? Commented Apr 8, 2016 at 17:03
  • Please avoid artificially adding tags to your question titles. See What are tags, and how should I use them?. Commented Apr 8, 2016 at 17:03

4 Answers 4

4

That's because you are adding the same object over and over. If you want different Lists to be stored, you need to use a new List on every iteration:

if (line != "100")
{
   var d = line.Split().Select(f => double.Parse(f, System.Globalization.NumberStyles.Float, CultureInfo.InvariantCulture));
   lista.Add(new List<double>(d));
}

If you add doubelowa, you are just adding the same reference over and over (and you are overwriting it on every iteration)


After your edit with the screenshot

Just in case the answer was not clear to you... when you add doublelowa to lista, you are just adding the same list every time.

So lista just keeps having the same object on every element:

  • lista[0] points to doublelowa
  • lista[1] points to doublelowa
  • lista[2] points to doublelowa
  • etc.

So if you clear doublelowa at any point, all elements of lista will point to the same, empty list. The solution, as I wrote above, is having each element be a different list, not doublelowa, which can be achieved with the code I wrote (and you can disregard doublelowa completely since it's not needed anymore).

Sign up to request clarification or add additional context in comments.

1 Comment

No problem, as long as you understood the problem/solution well enough (again: it has nothing to do with garbage collecting, so if you understood that, I may need to explain it further or more detailed)
0

What I believe is happening is that you only ever make one List object, which you make lista point to over and over again. That way, by changing doubelowa changes them all, because they are all actually the same object. To correct this, try replacing doubelowa.Clear(); with doubelowa = new List<double>();.

1 Comment

Yeah, my answer was probably not pretty enough? :-)
0

Jcl thank you for your explanation. I fix it with lucky try as you can se below. But without you i dont think that about references.

This happens probably because the reference is to every cell and while loop is completed Garbage Collector not cleaning up? Please if you want.explain me this in the detail.

                if (line != "100")
                {
                    List<double> doubelowa = new List<double>();
                    doubelowa.AddRange(line.Split().Select(f => double.Parse(f, System.Globalization.NumberStyles.Float, CultureInfo.InvariantCulture)));
                    lista.Add(doubelowa);
                }

3 Comments

This is exactly what I wrote in my answer. You are creating a new list on every iteration (you have called it doublelowa, and I just didn't create a variable for it since it's not needed, but same principle). This has nothing to do with the garbage collector. If you don't understand anything don't hesitate to ask and I'll try to explain in more detail
I implicated GC in this loop beacuse i start think when the references disapere from this list and stay only clear values. My question is. When i return "lista" in its cells be only clear valuse? When references dispared and stay only values?
as long as your lists are used by any other object, or are in a variable in scope, they won't get GCed. The lists you are adding to lista are used by lista, and the variable the function returns (which is the object pointed to by lista), will be in scope of the caller, as long as you are using it (and if assigned to a variable there, it may be longer within use). Once you are not using that object any more, it'll eventually get GCed
0

you can also create new list by ToList method of IEnumerator after splitting the line. concise your code by using Linq methods. List> r=File.ReadAllLines(fileName).SkipWhile((line) => line=="100") .Select((line){ line.Split().Select(f => double.Parse(f, System.Globalization.NumberStyles.Float, CultureInfo.InvariantCulture)).ToList()).ToList();

1 Comment

Thanks i try other method to!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.