Reputation: 1
I write static method wich read scientific numbers(X,Y) from text file and put it to List of list. But i dont 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 write this method and its work great. But now when i write more and more code i dont 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?
Upvotes: 0
Views: 54
Reputation: 1071
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();
Upvotes: 0
Reputation: 1
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);
}
Upvotes: 0
Reputation: 28272
That's because you are adding the same object over and over. If you want different List
s 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
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).
Upvotes: 4
Reputation: 149
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>();
.
Upvotes: 0