Reputation: 2094
I have a list of objects that contains several lists with strings. When I display a list of objects using foreach cycle, everything appears as it should, but when I try to display the two objects out of the loop in the console I see only the first object. I will be grateful for helping
items = new List<Object>();
items = fileOp.GetAnylisedCollection(PATH);
foreach (Object lists in items)
{
foreach (String list in (List<String>)lists)
{
Console.Write(list + " | ");
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine();
List<string> check = (List<string>)items[0];
Console.WriteLine(check[0] + check[1]);
Function which return List:
public List<Object> GetAnylisedCollection(String path)
{
int count = 0;
int prevSeparate = 0;
string line;
string toList = String.Empty;
List<Object> items = new List<Object>();
List<string> item = new List<string>();
StreamReader sr = new StreamReader(path);
//reading file line by line to string array
string[] str = new string[200];
while ((line = sr.ReadLine()) != null)
{
str[count] = line;
count++;
}
Array.Resize(ref str, count);
//array analysis and writing to collection
for (int i = 0; i < str.Length; i++)
{
char[] temp = str[i].ToCharArray();
for (int j = 0; j < temp.Length; j++)
{
toList = string.Empty;
if (temp[j].Equals('|'))
{
if (prevSeparate != 1) prevSeparate++;
for (int k = prevSeparate; k < j; k++)
{
toList += temp[k].ToString();
}
Console.WriteLine(toList);
item.Add(toList);
prevSeparate = j;
}
}
items.Add(item);
item = new List<string>();
Console.WriteLine();
}
return items;
}
Console output:
//output using foreach | Boston Logan International (BOS) | New York John F Kennedy (JFK) | 1200 | 10.11.2013 | | Boston Logan International (BOS) | Sacramento International (SMF) | 430 | 10.11.2013 | | Cleveland Hopkins International (CLE) | Sacramento International (SMF) | 543 |1.11.2013| | Beijing Capital (PEK) | New York John F Kennedy (JFK) | 2500 | 13.11.2013 | | Moscow Domodedovo (DME) | Boston Logan International (BOS) | 1230 | 15.11.2013 | | Washington Ronald Reagan (DCA) | Durango La Plata (DRO) | 340 | 14.11.2013 | | Atlanta Hartsfield-Jackson ATL (ATL) | Washington Ronald Reagan (DCA) | 450 | 7.11.2013| | Sacramento International (SMF) | Atlanta Hartsfield-Jackson ATL (ATL) | 325 | 6.11.2013| | New York John F Kennedy (JFK) | Beijing Capital (PEK) | 2300 | 19.11.2013 | | Cleveland Hopkins International (CLE) | New York John F Kennedy (JFK) | 360 | 2.11.2013| //simple output Boston Logan International (BOS)
This was to be displayed as:
Boston Logan International (BOS) New York John F Kennedy (JFK)
Upvotes: 1
Views: 1486
Reputation: 216291
Something is not how you suppose. Look at the first char on your lines. It is a pipe, but this char should never be printed at the start of your line, but only after the first item extracted from the first list.
Your first line should be (the same for the next lines)
Boston Logan International (BOS) | New York John F Kennedy (JFK) | 1200 | 10.11.2013 |
without the pipe character.
So perhaps your first list of list contains a first element that contains an empty space
you could try this code to prove this
List<string> check = (List<string>)items[0];
if(check.Count > 2)
Console.WriteLine(check[1] + check[2]);
Upvotes: 2
Reputation: 8791
Take a look at this line:
| Boston Logan International (BOS) | New York John F Kennedy (JFK) | 1200 | 10.11.2013 |
And now take a look at this code:
foreach (String list in (List<String>)lists)
{
Console.Write(list + " | ");
}
Console.WriteLine();
The first string is empty and therefore you have the or sign displayed.
Upvotes: 1